如何从图库中选择图像或捕获照片,然后在另一个活动中显示完整质量 [英] How to select an image from gallery or to capture a photo and then to display it in another activity full quality

查看:638
本文介绍了如何从图库中选择图像或捕获照片,然后在另一个活动中显示完整质量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个图像按钮,用于相机和画廊。如果选择相机,您可以从相机捕获照片,然后该照片必须在图像视图中的另一个活动中显示。如果您选择图库,那么您必须从图库中选择一张照片,然后该照片必须显示在另一个活动。我的代码库工作正常。我创建了两个其他活动(具有相同的布局),一个为画廊和其他相机。但是相机的代码不工作。这里是我的代码:
MainActivity:

I have two image buttons,for camera and gallery . If you select "camera" you can capture a photo from camera and then that photo has to be displayed in another activity in image view. If you select "gallery" then you have to select a photo from gallery and then that photo has to be display it in another activity . My code for gallery works fine . I created two others activities(with the same layout), one for gallery and the other for camera. But the code for camera doesn't work . Here is my code : 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(这是图库的活动):

ShowPhotoActivity(this is the activity for gallery):

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

    if (showPhoto != null) {


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


        showPhoto.setImageBitmap(bm); }

ShowCameraPhotoActivity(这是相机的活动):

ShowCameraPhotoActivity(this is the activity for camera):

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

manifest.xml:

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()。但我不知道什么。如果您有任何解决方案或更好的想法,请帮助我:)

I tried with just one other activity,but that also didn't work. In logcat it says that it happens because getRealPathFromUri(). But I don't know what . If you have any solutions or better ideas,please help me :)

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


推荐答案

您的代码包括:


  • getRealPathFromURI()在这种情况下一般和不必要。将 Uri 本身传递给 ShowPhotoActivity

  • getRealPathFromURI() is unreliable in general and unnecessary in this case. Pass the Uri itself to ShowPhotoActivity.

ShowPhotoActivity 正在主应用程序线程上加载图像。不要这样做。请使用许多 Android版图片加载库之一,因为他们可以使用 Uri

ShowPhotoActivity is loading the image on the main application thread. Do not do this. Please use one of many image loading libraries available for Android, as they can load your image asychronously using the Uri. Personally, I use Picasso.

您假设您从<$ c>返回 Uri $ c> ACTION_IMAGE_CAPTURE 。这不是 ACTION_IMAGE_CAPTURE 的工作原理。

You are assuming that you get a Uri back from ACTION_IMAGE_CAPTURE. That is not how ACTION_IMAGE_CAPTURE works.

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

其中:

    Bitmap bitmapImage = (Bitmap)data.getExtra("data");

这篇关于如何从图库中选择图像或捕获照片,然后在另一个活动中显示完整质量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆