无法在片段中设置图库中的图像
Unable to set the image from Gallery in a fragment
最近,我正在制作一个简单的应用程序,但由于一个我在网上了解到的小问题而被困了一段时间。
我正在尝试将 android 中图库中的图像设置到片段中的 imageView,但是,每当我 select 图像时,应用程序就会关闭。我已阅读此站点中的类似问题,但该解决方案无法解决我的问题。也许,在不知道的情况下,我可能设置了不同的东西,但我看不到代码的问题,我在下面设置了一个示例代码。
((Button) rootView.findViewById(R.id.Button01))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
//I want to set the image in this imageView
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
cursor.close();
} else {
}
//在你的manifest.xml文件中使用这个权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
//将此代码用于意图...
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(intent, "Select File"),REQUEST_GET_PHOTO);
//onResultActivity:
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
System.out.println("Get the Image from data");
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String imgDecodableString = cursor.getString(columnIndex);
cursor.close();
ImageView imgView = (ImageView) findViewById(R.id.imgView);
// Set the Image in ImageView after decoding the String
imgView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));
}
最近,我正在制作一个简单的应用程序,但由于一个我在网上了解到的小问题而被困了一段时间。
我正在尝试将 android 中图库中的图像设置到片段中的 imageView,但是,每当我 select 图像时,应用程序就会关闭。我已阅读此站点中的类似问题,但该解决方案无法解决我的问题。也许,在不知道的情况下,我可能设置了不同的东西,但我看不到代码的问题,我在下面设置了一个示例代码。
((Button) rootView.findViewById(R.id.Button01))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
//I want to set the image in this imageView
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
cursor.close();
} else {
}
//在你的manifest.xml文件中使用这个权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
//将此代码用于意图...
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(intent, "Select File"),REQUEST_GET_PHOTO);
//onResultActivity:
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
System.out.println("Get the Image from data");
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String imgDecodableString = cursor.getString(columnIndex);
cursor.close();
ImageView imgView = (ImageView) findViewById(R.id.imgView);
// Set the Image in ImageView after decoding the String
imgView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));
}