Java 从URI获取多个图像的实际路径
我对android和编码非常陌生,我被困在这里。我有一个gridview,它显示我SD卡上的图像。但我的gridview没有显示任何内容。 我的Uri如下所示:Java 从URI获取多个图像的实际路径,java,android,uri,android-gallery,Java,Android,Uri,Android Gallery,我对android和编码非常陌生,我被困在这里。我有一个gridview,它显示我SD卡上的图像。但我的gridview没有显示任何内容。 我的Uri如下所示: [content://com.android.providers.media.documents/document/image%3A252,content://com.android.providers.media.documents/document/image%3A68,content://com.android.providers
[content://com.android.providers.media.documents/document/image%3A252,content://com.android.providers.media.documents/document/image%3A68,content://com.android.providers.media.documents/document/image%3A67,content://com.android.providers.media.documents/document/image%3A172]
我已经尝试了很多方法将这个uri转换为实际路径,但都不管用
我尝试了此代码,但它导致我的应用程序崩溃:
public String getRealPathFromURI (Uri contentUri) {
String path = null;
String[] proj = { MediaStore.MediaColumns.DATA };
Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
if (cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
path = cursor.getString(column_index);
}
cursor.close();
return path;
}
这是我打开图库的方式:
btnGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
}
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_IMAGE);
}
});
此onActivityResult()方法代码:
if (requestCode == PICK_IMAGE_MULTIPLE && resultCode == RESULT_OK && null != data) {
// Get the Image from data
String[] filePathColumn = {MediaStore.Images.Media.DATA};
imagesEncodedList = new ArrayList<String>();
if (data.getData() != null) {
Uri mImageUri = data.getData();
// Get the cursor
Cursor cursor = getContentResolver().query(mImageUri, filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imageEncoded = cursor.getString(columnIndex);
cursor.close();
ArrayList<Uri> mArrayUri = new ArrayList<Uri>();
mArrayUri.add(mImageUri);
uriImage = mArrayUri.toString();// this string i store in sqlite
galleryAdapter = new GalleryAdapter(getApplicationContext(), mArrayUri);
gvGallery.setAdapter(galleryAdapter);
gvGallery.setVerticalSpacing(gvGallery.getHorizontalSpacing());
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) gvGallery.getLayoutParams();
mlp.setMargins(0, gvGallery.getHorizontalSpacing(), 0, 0);
} else {
if (data.getClipData() != null) {
ClipData mClipData = data.getClipData();
ArrayList<Uri> mArrayUri = new ArrayList<Uri>();
for (int i = 0; i < mClipData.getItemCount(); i++) {
ClipData.Item item = mClipData.getItemAt(i);
Uri uri = item.getUri();
mArrayUri.add(uri);
uriImage = mArrayUri.toString();//this string store in sqlite
// Get the cursor
Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imageEncoded = cursor.getString(columnIndex);
imagesEncodedList.add(imageEncoded);
cursor.close();
galleryAdapter = new GalleryAdapter(getApplicationContext(), mArrayUri);
gvGallery.setAdapter(galleryAdapter);
gvGallery.setVerticalSpacing(gvGallery.getHorizontalSpacing());
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) gvGallery
.getLayoutParams();
mlp.setMargins(0, gvGallery.getHorizontalSpacing(), 0, 0);
}
Log.v("LOG_TAG", "Selected Images" + mArrayUri.size());
}
}
} else if (resultCode == this.RESULT_CANCELED) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
}
并发送至共享优先权:
SharedPreferences settings = mCtx.getSharedPreferences("image_My_str", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("img_str", image_string);
editor.commit();
在我的显示活动中,我从共享首选项中获取以下字符串:
SharedPreferences settings = getSharedPreferences("image_My_str", Context.MODE_PRIVATE);
String encodedImage = settings.getString("img_str", "");
在这里,我将该字符串转换为URi:
Uri mImageUri = Uri.parse(encodedImage);
Log.d("LOG_TAG", "Images URI " + mImageUri);
try {
inputStream = getApplicationContext().getContentResolver().openInputStream(mImageUri);
Log.d("LOG_TAG", "Images File " + inputStream);
bitmap = BitmapFactory.decodeStream(inputStream);
Log.d("LOG_TAG", "Images bitmap " + bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (bitmap != null) {
img.setImageBitmap(bitmap);
}else {
Toast.makeText(EditMapsActivity.this, "image not found", Toast.LENGTH_LONG).show();
}
我的URI看起来像:
[content://com.android.providers.media.documents/document/image%3A252,content://com.android.providers.media.documents/document/image%3A68,content://com.android.providers.media.documents/document/image%3A67,content://com.android.providers.media.documents/document/image%3A172]
尝试了多种方法将此uri转换为实际路径
。你不应该一开始就这样做。直接使用这些uri即可。例如,打开一个输入流。但是如何打开?我不明白。InputStream是=getContentRecolver()。openInputStream(uri)。它提供空值。请查看截图。查看我的最新帖子。我张贴相关代码
[content://com.android.providers.media.documents/document/image%3A252,content://com.android.providers.media.documents/document/image%3A68,content://com.android.providers.media.documents/document/image%3A67,content://com.android.providers.media.documents/document/image%3A172]