0

カメラを使用して画像をキャプチャし、画像パスが必要です。

次のコードを試してみましたが、エラーが発生しました。(私はこのリンクGet Path of image from ACTION_IMAGE_CAPTURE Intentに従います)

これが私のコードです。

MainActivity.java

public class MainActivity  extends Activity {
 private static final int CAMERA_REQUEST = 1888; 
    private ImageView imageView;
    private Uri mCapturedImageURI;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.imageView = (ImageView)this.findViewById(R.id.imageView1);
        Button photoButton = (Button) this.findViewById(R.id.button1);
        photoButton.setOnClickListener(new View.OnClickListener() {



            @Override
            public void onClick(View v) {


                 String fileName = "temp.jpg";  
                 ContentValues values = new ContentValues();  
                 values.put(MediaStore.Images.Media.TITLE, fileName);  
                 mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  

                 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
                 intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);  
                 startActivityForResult(intent, CAMERA_REQUEST);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  

             String[] projection = { MediaStore.Images.Media.DATA}; 
             Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null); 
             int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
             cursor.moveToFirst(); 
             String capturedImageFilePath = cursor.getString(column_index_data);

             Toast.makeText(getApplicationContext(), capturedImageFilePath.toString(), Toast.LENGTH_LONG).show();

        }  
    } 
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="photo" >
    </Button>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" >
    </ImageView>

</LinearLayout>

AndroidManifest.xml

<uses-feature android:name="android.hardware.camera"></uses-feature>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

エラー

java.lang.RuntimeException: Unable to resume activity {com.example.cameraupload/com.example.cameraupload.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=null} to activity {com.example.cameraupload/com.example.cameraupload.MainActivity}: java.lang.NullPointerException
4

2 に答える 2

0

ついに私はAnsを手に入れました。

photoButton.setOnClickListener(new OnClickListener(){

        public void onClick(View v) {

           File file = new File(Environment.getExternalStorageDirectory()+"/test1.jpg");
            Uri outputFileUri = Uri.fromFile(file);
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(intent, 0);

        }
    });



protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.i("MakeMachine", "requestCode:"+requestCode + ",resultCode: " + resultCode);

                        File file = new File(Environment.getExternalStorageDirectory()+"/test1.jpg");
                        if(file.exists()){
                            BitmapFactory.Options options = new BitmapFactory.Options();
                            options.inSampleSize = 4;
                            picFileName=Environment.getExternalStorageDirectory()+"/test.jpg";
                            Toast.makeText(getApplicationContext(), picFileName, Toast.LENGTH_LONG).show();
                            Bitmap bitmap = BitmapFactory.decodeFile(picFileName, options);
                            imageView.setImageBitmap(bitmap);
                            //imageView.setVisibility(View.VISIBLE);
                        }


    }
于 2012-11-28T10:21:57.267 に答える
0
if (resultCode != RESULT_CANCELED) {
    if (requestCode == CAMERA_REQUEST) {

     String[] projection = { MediaStore.Images.Media.DATA };
      Cursor cursor = managedQuery(mCapturedImageURI, projection,                   null, null, null);
                int column_index_data = cursor
                        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                String capturedImageFilePath = cursor
                        .getString(column_index_data);

                Toast.makeText(getApplicationContext(),
                        capturedImageFilePath.toString(), Toast.LENGTH_LONG)
                        .show();

            }
        }

あなたはこれを試してみてください。彼らはあなたを助けると思います..

于 2012-11-28T07:31:28.107 に答える