简体   繁体   English

从文件路径将位图设置为imageview不起作用

[英]Setting bitmap to imageview from filepath is not working

I tried almost all methods possible to set image to a image-view from stored file path. 我尝试了几乎所有可能的方法来从存储的文件路径将图像设置为image-view

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 图片类型为 .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. 当我尝试使用此代码将图像设置为imageview ,没有错误,但未设置图像。

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 ? 如何将路径格式为/external/storage/media/carimagename的图像设置为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? 我从selectedImagePath得到错误的路径吗?

this is the path i got using this which had no mnt/sdcard 这是我使用的没有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();

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"/>

Take a gander at this . 对此轻描淡写。

You can use the ImageView.setImageURI(..) methode. 您可以使用ImageView.setImageURI(..)方法。

preview.setImageURI(Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Echo/Images/"+file_name));
Image paths stored in database with paths without any extensions

You need to mention image type in path. 您需要在路径中提及图片类型。 e. e。 image.png 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);

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

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