gpt4 book ai didi

android - 从图库中选取图像并设置为 imageview

转载 作者:行者123 更新时间:2023-11-29 17:30:58 27 4
gpt4 key购买 nike

我正在尝试从图库中选择图像并将其设置为 imageview。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {

Log.e("1","1");
// Get the Image from data

Uri selectedImage = data.getData();
Log.e("2","2");

String[] filePathColumn = { MediaStore.Images.Media.DATA };
Log.e("3","3");

// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
Log.e("4","4");
// Move to first row
cursor.moveToFirst();
Log.e("5", "5");

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
Log.e("6","6");
imgDecodableString = cursor.getString(columnIndex);
Log.e("7","7");
cursor.close();
ImageView imgView = (ImageView) findViewById(R.id.imgView);
Log.e("8","8");
// Set the Image in ImageView after decoding the String
imgView.setImageBitmap(BitmapFactory
.decodeFile(imgDecodableString));

} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}

}

在日志中它只显示第三个日志,然后显示捕获的 toast。我用的是三星S4,安卓版本是5.0.1。我认为,问题出在光标上,但无法修复。请帮助我。

最佳答案

这是我用于选择图库图像然后将其显示到 ImageView 的代码。您可以根据需要使用。

这里是打开 Gallery Intent 的方法。

private static final int GALLERY_PHOTO = 111;

private void pickGalleryImage() {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(chooserIntent, GALLERY_PHOTO);
}

那么你的onActivityResult() 方法应该是这样的。在此方法中,mContext 指的是 Activity 的上下文。如果 Activity 名称是 MainActivity 那么您可以使用 MainActivity.this

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_PHOTO && resultCode == Activity.RESULT_OK) {
if (data.getData() != null) {
String filePath = GetFilePathFromDevice.getPath(mContext, data.getData());
Bitmap bm = BitmapFactory.decodeFile(filePath);
imgView.setImageBitmap(bm);
}
}
}

这里是 GetFilePathFromDevice 类,在 onActivityResult() 方法中用于从 Gallery 中获取所选图像的文件路径。

public final class GetFilePathFromDevice {

/**
* Get file path from URI
*
* @param context context of Activity
* @param uri uri of file
* @return path of given URI
*/
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{split[1]};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}

public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {column};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}

public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}

public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}

public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}

public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
}

希望对你有帮助。

关于android - 从图库中选取图像并设置为 imageview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33011148/

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