简体   繁体   中英

Specify Gallery image for imageview

first time poster & very new to Android/Java. This board has been a great help, thanks!

I'm having problems trying to use a string to select an exact filename for an image in my device gallery and display it in an imageview - (not browse/pick from the gallery, that works fine) and avoiding deprecated methods like startactivityforresult.

I've set Read permissions for external storage and checked the image & pathname is correct using cx file explorer, but I just can't get the image to load. I've also been trying it with Glide, trying different ways, as below. (Glide works ok when displaying a mipmap image).

I can get the Toast message to show the right path/name, but still nothing in the imageview.

// File myfilename = new File(Environment.getExternalStorageDirectory().getPath()+"/DCIM/my_gallery_image.png");
// File myfilename = new File(Environment.getExternalStorageDirectory().getPath(), "/my_gallery_image.png");
// File myfilename = new File(Environment.getExternalStorageDirectory().toString(),"/images/my_gallery_image.png");
// File myfilename = new File(getExternalCacheDir() + "/images/my_gallery_image.png");
// File myfilename = new File ("/mnt/sdcard/0" + "/images/my_gallery_image.png");
// File myfilename = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "/images/my_gallery_image.png");
// File myfilename = new File(Environment.getExternalStorageDirectory().getPath(), "/images/my_gallery_image.png");
// Uri myfilename = Uri.fromFile( new File(Environment.getExternalStorageDirectory().getPath()+"/images/my_gallery_image.png"));
//
// GlideApp
// .with(context)
// .load(file)
// .into(imageViewFile);
//
// File myfilename = new File(Environment.getExternalStorageDirectory()+File.separator +"/DCIM/my_gallery_image.png");

// Uri imageUri = Uri.fromFile(myfilename);


// Glide.with(this)
// .load(new File(Environment.getExternalStorageDirectory().getPath(), "/images/my_gallery_image.png"))
// .load (Uri.parse(myfilename))
// .load(Uri.fromFile(myfilename))
// .load (imageUri.getPath())
// .load(imageUri)
// .load(myfilename)
// .load(Uri.parse (myfilename))
// .error(R.mipmap.image_error)
// .diskCacheStrategy(DiskCacheStrategy.ALL) 
// .into(imageView_H);

// Toast toastf = Toast.makeText(getApplicationContext(), "= " + imageUri.getPath(), Toast.LENGTH_LONG);
// toastf.show();

Any ideas please?

File imgFile = new File("/sdcard/Images/test_image.jpg");

if(imgFile.exists()) {

Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

myImage.setImageBitmap(myBitmap);

}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM