简体   繁体   English

如何在图像视图的另一个活动中加载从相机捕获的图片?

[英]how to load a picture captured from camera in another activity on image view?

I have two image buttons,one is for taking a photo from gallery and display it in another activity. 我有两个图像按钮,一个用于从图库中拍照并在另一项活动中显示。 This works fine. 这很好。 It displays the photo with high quality . 它以高质量显示照片。 But when i try to take a picture from camera and then that photo to be displayed in another activity,it shows an error. 但是,当我尝试从相机拍摄照片然后将该照片显示在其他活动中时,它显示了错误。 Here is my code MainActivity.java: 这是我的代码MainActivity.java:

private static final int REQUEST_CAMERA = 1;
private static int SELECT_FILE = 1;
public static Uri selectedImageURI;

if (take_photo != null) {
        take_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent,REQUEST_CAMERA);

            }
        });
    }
    //to get the photo from gallery
    if (get_photo != null) {
        get_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i,SELECT_FILE);


}

if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_FILE) {
            selectedImageURI = data.getData();
            File imageFile = new File(getRealPathFromURI(selectedImageURI));
             Intent intent = new Intent(this, ShowPhotoActivity.class);
             intent.putExtra("imagePath", imageFile.toString());
             startActivity(intent);}
        else if (requestCode != RESULT_CANCELED)
        {
            if(requestCode == REQUEST_CAMERA){
                Uri selectImageURI = data.getData();
                File imgFile = new File(getRealPathFromURI(selectImageURI));
                Intent i = new Intent(this,ShowPhotoActivity.class);
                i.putExtra("imagePath",imgFile.toString());
                startActivity(i);

            }
        } }}
        private String getRealPathFromURI(Uri contentURI){
            String result;
            Cursor cursor = getContentResolver().query(contentURI,null,null,null,null);
            if(cursor == null){
                result = contentURI.getPath();

            }else {
                cursor.moveToFirst();
                int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                result = cursor.getString(idx);
                cursor.close();
            }
        return result;
    }

showPhotoActivity.java: showPhotoActivity.java:

String image_path = getIntent().getStringExtra("imagePath");
 Bundle extras = getIntent().getExtras();
 Uri uri = Uri.parse(extras.getString("imagePath"));

    if (showPhoto != null) {

        Bitmap bm = BitmapFactory.decodeFile(uri.getPath());
        showPhoto.setImageBitmap(bm);
    }

manifest.xml manifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

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

logcat: 日志猫:

 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.gentaliu.photoeditor/com.example.gentaliu.photoeditor.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
                                                                                  at android.app.ActivityThread.deliverResults(ActivityThread.java:3840)
                                                                                  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3883)
                                                                                  at android.app.ActivityThread.access$1700(ActivityThread.java:165)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1426)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:135)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5593)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
                                                                                  at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1477)
                                                                                  at android.content.ContentResolver.query(ContentResolver.java:473)
                                                                                  at android.content.ContentResolver.query(ContentResolver.java:426)
                                                                                  at com.example.ga.photoeditor.MainActivity.getRealPathFromURI(MainActivity.java:96)
                                                                                  at com.example.ga.photoeditor.MainActivity.onActivityResult(MainActivity.java:69)
                                                                                  at android.app.Activity.dispatchActivityResult(Activity.java:6320)
                                                                                  at android.app.ActivityThread.deliverResults(ActivityThread.java:3836)
                                                                                  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3883) 
                                                                                  at android.app.ActivityThread.access$1700(ActivityThread.java:165) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1426) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                  at android.os.Looper.loop(Looper.java:135) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5593) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

09-16 00:46:51.149 27066-27066/com.example.ga.photoeditor I/Process: Sending signal. 09-16 00:46:51.149 27066-27066 / com.example.ga.photoeditor I / Process:正在发送信号。 PID: 27066 SIG: 9 PID:27066 SIG:9

private static final int REQUEST_CAMERA = 1;
private static int SELECT_FILE = 1;
public static Uri selectedImageURI;

if (take_photo != null) {
        take_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent,REQUEST_CAMERA);
            }
        });
    }

    //to get the photo from gallery
    if (get_photo != null) {
        get_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i,SELECT_FILE);
       }
    });
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_FILE) {
            selectedImageURI = data.getData();
            File imageFile = new File(getRealPathFromURI(selectedImageURI));
            Intent intent = new Intent(this, ShowPhotoActivity.class);
            intent.putExtra("imagePath", imageFile.toString());
            startActivity(intent);
            }

        else if(requestCode == REQUEST_CAMERA){
            Bitmap bitmapImage = (Bitmap) intent.getExtras().get("data");
            Intent i= new Intent(this,ShowPhotoActivity.class);
            i.putExtra("bitmap", bitmapImage);
            startActivity(i);

 }else{
       //do something 
      }
 }

private String getRealPathFromURI(Uri contentURI){
            String result;
            Cursor cursor = getContentResolver().query(contentURI,null,null,null,null);
            if(cursor == null){
                result = contentURI.getPath();

            }else {
                cursor.moveToFirst();
                int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                result = cursor.getString(idx);
                cursor.close();
            }
        return result;
    }

And retrieve the Bitmap Image of Camera in second Activity: 并在第二个活动中检索“相机”的位图图像:

private void getData(){

    Intent intent = getIntent();
    Bitmap bitmap = (Bitmap) intent.getParcelableExtra("Bitmap");
    img.setImageBitmap(bitmap);

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将捕获的图像从自定义相机传递到另一个活动? - How to pass a captured image from custom camera to another activity? 将捕获的图像从Camera2 Api传递到另一个活动 - Passing captured Image from Camera2 Api to another Activity 如何将捕获/保存的图像从相机获取到另一个应用程序? - How to get the captured / saved image from camera to another app? 如何将相机拍摄的图像存储在图库中? - How to store image captured from camera in gallery? 如何在相机捕获的图像上叠加图像或视图 - How to overlay an image or view on top of a camera captured image 如何将从相机或画廊获得的图像传递到另一个活动 - How to pass image gotten from camera or gallery to another activity 如何在另一个活动中调用捕获的图像{不需要它显示} - How to call captured image in another activity{Don't need it to be displayed} 如何调整从相机捕获的图像大小并显示在imageview上 - How to resize image that captured from camera and display on imageview 在另一个活动中将从图库中选择或从相机捕获的图像添加到 gridview - Add images selected from gallery or captured from camera into a gridview in another activity Android 在 CircleImageView 中显示从相机捕获的图像 - Android display captured image from Camera in CircleImageView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM