问题
I tried almost all methods possible to set image to a image-view from stored file path.
None of them work. The images were selected from gallery in other activity and imagepath stored into database.
Images are of type .jpg .jpeg .png
Image paths stored in database with paths without any extensions
eg. /external/storage/media/carimagename
when I try to set image to imageview using this code there is no error but image is not set.
for(int i=0;i<n;i++)
{
Bitmap pic = BitmapFactory.decodeFile(listview_arraypro1[i]);
carpict.setImageBitmap(pic);
/// File imgFile =new File("listview_arraypro1[i]");
// carpict.setImageBitmap(BitmapFactory.decodeFile(imgFile.getAbsolutePath()));
// Bitmap carpic = BitmapFactory.decodeStream(is);
// carpic = Bitmap.createScaledBitmap(carpic, 72, 70, false);
}
How can i set the images with path format /external/storage/media/carimagename to an imageview?
Im using the following to get the image path in previous activity
Uri selectedImage = imageReturnedIntent.getData();
String selectedImagePath = selectedImage.getPath();
Am i getting the wrong path from selectedImagePath?
this is the path i got using this which had no mnt/sdcard
private String getRealPathFromURI(Uri contentURI) {
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file path
return contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
}
File imageFile = new File(getRealPathFromURI(selectedImage));
String path= imageFile.toString();
回答1:
The main thing is that first you have to need mentioned your image type in your image path...
And after then you can retrieve using that imagepath:
String img_path = "image will be with image type";
Bitmap bmp= BitmapFactory.decodeFile(img_path);
ImageView iv= (ImageView)findViewById(R.id.img);
iv.setImageBitmap(bmp);
you should add permission to manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
回答2:
Take a gander at this.
You can use the ImageView.setImageURI(..) methode.
preview.setImageURI(Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Echo/Images/"+file_name));
回答3:
Image paths stored in database with paths without any extensions
You need to mention image type in path. e. image.png
You can try like following example.
String imagePath = "your image path with image type"; //eg. /sdcard/img.PNG
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
ImageView myImageView = (ImageView)findViewById(R.id.imageview);// your imageview
myImageView.setImageBitmap(bitmap);
来源:https://stackoverflow.com/questions/18459941/setting-bitmap-to-imageview-from-filepath-is-not-working