如何从图库中获取所选图像的路径? [重复]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从图库中获取所选图像的路径? [重复]相关的知识,希望对你有一定的参考价值。
我需要存储从图库中选择的图像的路径。在吐司中,我得到了琴弦imageEncoded = null。我也有一个List变量imageEncodedList,它在Toast中也给出了“ null”选择多个图像时。我做错了什么?我想将所选图像的路径存储在android中。我还需要为API级别<18从图库中选择图像吗?
int SELECT_PICTURES=1;
String imageEncoded;
List<String> imagesEncodedList;
select_image.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURES);
);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
try
// When an Image is picked
if (requestCode == SELECT_PICTURES && 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 = this.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();
Toast.makeText(MainActivity.this,"one "+imageEncoded,Toast.LENGTH_LONG).show();
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);
// 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);
Toast.makeText(MainActivity.this,"two"+imageEncoded,Toast.LENGTH_LONG).show();
imagesEncodedList.add(imageEncoded);
cursor.close();
else
Toast.makeText(this, "You haven't selected any Image",
Toast.LENGTH_LONG).show();
catch (Exception e)
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
答案
在manifest.xml中添加权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
对于getClipData
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);
Toast.makeText(this, "" + getImageFilePath(uri), Toast.LENGTH_SHORT).show();
对于图像路径:
public String getImageFilePath(Uri uri)
File file = new File(uri.getPath());
String[] filePath = file.getPath().split(":");
String image_id = filePath[filePath.length - 1];
Cursor cursor = getContentResolver().query(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Images.Media._ID + " = ? ", new String[]image_id, null);
if (cursor != null)
cursor.moveToFirst();
String imagePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
return imagePath;
return null;
以上是关于如何从图库中获取所选图像的路径? [重复]的主要内容,如果未能解决你的问题,请参考以下文章