简体   繁体   English

为 imageview 指定图库图片

[英]Specify Gallery image for imageview

first time poster & very new to Android/Java.第一次发布海报,对 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.我在尝试使用字符串为我的设备库中的图像选择确切的文件名并将其显示在图像视图中时遇到问题 - (不是从库中浏览/选择,效果很好)并避免不推荐使用的方法,如 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.我已经为外部存储设置了读取权限,并使用 cx 文件资源管理器检查了图像和路径名是否正确,但我无法加载图像。 I've also been trying it with Glide, trying different ways, as below.我也一直在尝试使用 Glide,尝试不同的方式,如下所示。 (Glide works ok when displaying a mipmap image). (在显示 mipmap 图像时 Glide 可以正常工作)。

I can get the Toast message to show the right path/name, but still nothing in the imageview.我可以获得 Toast 消息以显示正确的路径/名称,但图像视图中仍然没有任何内容。

// 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");文件 imgFile = new File("/sdcard/Images/test_image.jpg");

if(imgFile.exists()) {如果(imgFile.exists()){

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

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

myImage.setImageBitmap(myBitmap);

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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