如何从图库中获取图像URI? [英] How to get Image URI from Gallery?

查看:64
本文介绍了如何从图库中获取图像URI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从图库中获取图像.我可以打开图库来选择图像,但是选择图像后它什么也没有返回.我需要将该 fileUri 发送到另一个活动并将其显示在ImageView上.我能够制作此相机,就像在按钮上单击它一样,将其称为相机,然后捕获图像并将其发送到另一个活动.

I need to fetch an image from a gallery. I'm able to open gallery to select a image, but after selecting the image it doesn't return anything. I need to send that fileUri to another activity and display it on ImageView. I'm able to do this camera, like on button click it call the camera and then I capture image and send it to another activity.

但是我不明白我在画廊中使用什么.有人请帮我.

But I don't understand what I use for gallery. Someone please help me with this.

更新

这是我用来从相机获取图像的方法

This is I'm using to get image from Camera

private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    // start the image capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}

这是m用来从图库中获取图像的方法但是我想要以与捕获image()相同的方式来执行此操作,以便可以将ImageUri发送到其他活动

 private void browseImage(){
    try {
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        // Start the Intent
         fileUri = getOutputMediaFileUri(PICK_IMAGE);//this is m using for camera 
         Log.w("ImageAddressOnClick pr", ""+fileUri);
        startActivityForResult(galleryIntent, GALLERY_IMAGE_PICK);
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(),
        e.getMessage()+"ye show hora h",
        Toast.LENGTH_LONG).show();
        Log.e(e.getClass().getName(), e.getMessage(), e);
    }

OnActivityResult方法

OnActivityResult Method

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // if the result is capturing Image
     super.onActivityResult(requestCode, resultCode, data);



    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {

            // successfully captured the image
            // launching upload activity
            launchUploadActivity(true);


        } else if (resultCode == RESULT_CANCELED) {

            // user cancelled Image capture
            Toast.makeText(getApplicationContext(),
                    "User cancelled image capture", Toast.LENGTH_SHORT)
                    .show();

        } else {
            // failed to capture image
            Toast.makeText(getApplicationContext(),
                    "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                    .show();
        }


    }
    else if(requestCode == GALLERY_IMAGE_PICK && resultCode == RESULT_OK
            && null != data)
            {
       Uri selectedImage = data.getData();
         String[] filePathColumn = { MediaStore.Images.Media.DATA };
         Log.w("onActivityResult", "chali ye onActivityResult "+selectedImage);
         // Get the cursor
         Cursor cursor = getContentResolver().query(selectedImage,
                 filePathColumn, null, null, null);
         // Move to first row
         cursor.moveToFirst();

         //int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
       //  imgDecodableString = cursor.getString(columnIndex);
         cursor.close();
         launchUploadActivity(true);
            }
          }

private void launchUploadActivity(boolean isImage){
    Intent i = new Intent(MainActivity.this, UploadActivity.class);
    i.putExtra("filePath", fileUri.getPath());
    i.putExtra("isImage", isImage);
    startActivity(i);
}

/**
 * ------------ Helper Methods ---------------------- 
 * */

/**
 * Creating file uri to store image/video
 */
public Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

/**
 * returning image / video
 */
private static File getOutputMediaFile(int type) {

    // External sdcard location
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            Config.IMAGE_DIRECTORY_NAME);

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(TAG, "Oops! Failed create "
                    + Config.IMAGE_DIRECTORY_NAME + " directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;
}

我正在通过launchUploadActivity()发送数据

I'm sending data through launchUploadActivity();

先谢谢了:)

推荐答案

onActivityResult中的更改:

Change in onActivityResult:

else if(requestCode == GALLERY_IMAGE_PICK && resultCode == RESULT_OK
        && null != data)
        {
            Uri selectedImage = data.getData();         
            String picturePath = getRealPathFromURI(selectedImage,
                    this);
           Intent i = new Intent(MainActivity.this, UploadActivity.class);
           i.putExtra("filePath", selectedImage);
           i.putExtra("isImage", isImage);
           startActivity(i);
        }
      }
}


public String getRealPathFromURI(Uri contentURI, Activity context) {
        String[] projection = { MediaStore.Images.Media.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = context.managedQuery(contentURI, projection, null,
                null, null);
        if (cursor == null)
            return null;
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        if (cursor.moveToFirst()) {
            String s = cursor.getString(column_index);
            // cursor.close();
            return s;
        }
        // cursor.close();
        return null;
    }

这篇关于如何从图库中获取图像URI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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