gpt4 book ai didi

android - 如何从 Gallery 获取图片 URI?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:46:39 27 4
gpt4 key购买 nike

我需要从图库中获取图像。我可以打开图库来选择图像,但在选择图像后它不会返回任何内容。我需要将该 fileUri 发送到另一个 Activity 并将其显示在 ImageView 上。我可以做这个相机,就像点击按钮一样调用相机,然后我捕捉图像并将它发送到另一个 Activity 。

但我不明白我用画廊做什么。有人请帮我解决这个问题。

更新

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);
}

This is m using to fetch image from gallery But I want to do it in a same way as I'm doing for captureImage(), so that i can send ImageUri to other activity

 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 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;
}

I'm sending data through launchUploadActivity();

提前致谢:)

最佳答案

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", picturePath);
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;
}

关于android - 如何从 Gallery 获取图片 URI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32652762/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com