如何从图库中选择图像或捕获照片,然后以另一个完整质量的活动显示它

时间:2016-09-21 17:13:40

标签: android image android-intent camera android-bitmap

我有两个图像按钮,用于相机和图库。如果选择“相机”,则可以从相机拍摄照片,然后该照片必须显示在图像视图中的其他活动中。如果您选择“图库”,则必须从图库中选择一张照片,然后该照片必须在另一个活动中显示。我的画廊代码很好。我创建了另外两个活动(具有相同的布局),一个用于图库,另一个用于相机。但相机代码不起作用。这是我的代码: MainActivity:

private static final int REQUEST_CAMERA = 1;
private static int SELECT_FILE = 1;
 ImageButton take_photo = (ImageButton) findViewById(R.id.cameraButton);
    ImageButton get_photo = (ImageButton) findViewById(R.id.galleryButton);
    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);
   }
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, 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) {
                {
                    try {
                        Uri returnUri = data.getData();
                        Bitmap bitmapImage =    MediaStore.Images.Media.getBitmap(MainActivity.this.getContentResolver(), returnUri);
                        Intent i = new Intent(this, ShowCameraPhotoActivity.class);
                        i.putExtra("image", bitmapImage);
                        startActivity(i);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }  }
            }}
            } }

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(这是图库的活动):

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

    if (showPhoto != null) {


        Bitmap bm = BitmapFactory.decodeFile(uri.getPath());


        showPhoto.setImageBitmap(bm); }

ShowCameraPhotoActivity(这是相机的活动):

Bitmap bitmap = (Bitmap) this.getIntent().getParcelableExtra("image");
    if (showPhoto != null) {
        showPhoto.setImageBitmap(bitmap);
    }

的manifest.xml:

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

我尝试过另外一项活动,但这也没有用。在logcat中,它说它发生的原因是getRealPathFromUri()。但我不知道是什么。如果您有任何解决方案或更好的想法,请帮助我:)

logcat的:

 Process: com.example.gentaliu.photoeditor, PID: 22360
                                                                              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.gentaliu.photoeditor.MainActivity.getRealPathFromURI(MainActivity.java:189)
                                                                                  at com.example.gentaliu.photoeditor.MainActivity.onActivityResult(MainActivity.java:105)
                                                                                  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-21 19:11:44.293 22360-22360/com.example.gentaliu.photoeditor I/Process:     Sending signal. PID: 22360 SIG: 9

1 个答案:

答案 0 :(得分:1)

您的代码存在一些问题,包括:

  • getRealPathFromURI()一般来说不可靠,在这种情况下是不必要的。将Uri本身传递给ShowPhotoActivity

  • ShowPhotoActivity正在主应用程序线程上加载图像。不要这样做。请使用众多image loading libraries available for Android中的一个,因为他们可以使用Uri异步加载您的图片。就个人而言,我使用毕加索。

  • 您假设从[{1}}返回Uri。这不是ACTION_IMAGE_CAPTURE的工作方式。

相反,替换:

ACTION_IMAGE_CAPTURE

使用:

    Uri returnUri = data.getData();
    Bitmap bitmapImage =    MediaStore.Images.Media.getBitmap(MainActivity.this.getContentResolver(), returnUri);