I want to create an activity When I open the activity it to show images in specific folder called "myfolder" and when I select image I need it to show it in an imageview.
The code I created is working but it is showing me all images in my device and when I select image it return to the app but doesn't show the image in the imageview
ImageView imageview1;
private static final int REQUEST_OPEN_RESULT_CODE = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
imageview1 = (ImageView) findViewById(R.id.imageview);
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/myfolder/");
intent.setDataAndType(uri, "image/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_OPEN_RESULT_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData){
if (requestCode == REQUEST_OPEN_RESULT_CODE && requestCode == RESULT_OK){
Uri uri = null;
if(resultData != null){
uri = resultData.getData();
Glide.with(this)
.load(uri)
.into(imageview1);
}
}
}
private Bitmap getBitmapFromUri(Uri uri) throws IOException {
ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
return bitmap;
}
private GestureDetector gs = null;
Try to convert URI to Path
public static String getPathFromURI(Context context, Uri uri) {
String[] proj = { MediaStore.Images.Media.DATA };
String result = null;
CursorLoader cursorLoader = new CursorLoader(
context,
uri, proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
if(cursor != null){
int column_index =
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
result = cursor.getString(column_index);
}
return result;
}
and set path inside glide
may be work
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.