Android中的Android选择图片无效(Android 6.0& Android 5.1.1) [英] Android pick image from gallery not working (Android 6.0 & Android 5.1.1)
问题描述
我正在使用以下代码从设备库中选择一个图像文件:
首先我调用这段代码:
Intent i = new Intent();
i.setType(image / *);
i.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(i,Select Picture),RESULT_LOAD_IMAGE);
这是我的 onActivityResult
方法:
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult( requestCode,resultCode,data);
if(requestCode == RESULT_LOAD_IMAGE&& resultCode == RESULT_OK&& null!= data){
photoPath = getPath(data.getData()) ;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis;
try {
fis = new FileInputStream(new File(photoPath));
byte [] buf = new byte [1024];
int n;
while(-1!=(n = fis.read(buf))){
baos.write(buf,0,n);
}
img.setImageBitmap(BitmapFactory.decodeFile(photoPath));
} catch(例外e){
e.printStackTrace();
}
}
}
这个检索图像路径的辅助方法:
private String getPath(Uri uri){
String [ ] data = {MediaStore.Images.Media.DATA};
CursorLoader loader = new CursorLoader(getApplicationContext(),uri,data,null,null,null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
返回cursor.getString(column_index);
}
我的问题是应用程序很奇怪。在我的6.0模拟器中,有时它可以工作,有时候在其他设备(Android 5.1.1)中,此行抛出 FileNotFound异常
fis = new FileInputStream(new File(photoPath));
所有必填项权限很好。你们有什么想法在这里发生了什么?或者你有什么更好的建议用于从画廊中挑选图像?
试试这个你可以直接从onActivityResult设置图像
'pre>
@覆盖
保护无效onActivityResult(INT requestCode,INT发送resultCode,意图数据){
super.onActivityResult(requestCode,resultCode为,数据);
if(requestCode == PICK_IMAGE_REQUEST&& resultCode == RESULT_OK&& data!= null&& data.getData()!= null){
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver()。openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
imageView.setImageBitmap(selectedImage);
} catch(IOException e){
e.printStackTrace();
}
}
}
I'm using the code below to pick an image file from device gallery:
First I call this piece of code:
Intent i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(i, "Select Picture"), RESULT_LOAD_IMAGE);
This is my onActivityResult
method:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
photoPath = getPath(data.getData());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis;
try {
fis = new FileInputStream(new File(photoPath));
byte[] buf = new byte[1024];
int n;
while (-1 != (n = fis.read(buf))) {
baos.write(buf, 0, n);
}
img.setImageBitmap(BitmapFactory.decodeFile(photoPath));
} catch (Exception e) {
e.printStackTrace();
}
}
}
And this a helper method to retrieve image path:
private String getPath(Uri uri) {
String[] data = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(getApplicationContext(), uri, data, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
My problem is that the app is weird. In my 6.0 emulator, sometimes it works and sometimes No. In an other device (Android 5.1.1) an FileNotFound Exception
is thrown at this line
fis = new FileInputStream(new File(photoPath));
All required permissions are fine.Do you guys have any idea what's going on here? Or do you have any better suggestion what to use to pick an image from gallery?
Try this you can directly set image from onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
try{
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
imageView.setImageBitmap(selectedImage);
} catch (IOException e) {
e.printStackTrace();
}
}
}
这篇关于Android中的Android选择图片无效(Android 6.0& Android 5.1.1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!