【问题标题】:Setting bitmap to imageview from filepath is not working从文件路径将位图设置为 imageview 不起作用
【发布时间】:2013-08-27 07:54:05
【问题描述】:

我尝试了几乎所有可能的方法来从存储的文件路径将图像设置为image-view

它们都不起作用。 图像是从其他活动的图库中选择的,图像路径存储到数据库中。

图像的类型为 .jpg .jpeg .png

图像路径存储在数据库中,路径不带任何扩展名

eg.  /external/storage/media/carimagename

当我尝试使用此代码将图像设置为 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); 

 }

如何将路径格式/external/storage/media/carimagename 的图像设置为imageview

我正在使用以下内容获取上一个活动中的图像路径

             Uri selectedImage = imageReturnedIntent.getData();

           String  selectedImagePath = selectedImage.getPath();

我是不是从 selectedImagePath 得到了错误的路径?

这是我使用这个没有 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();

【问题讨论】:

    标签: java android xml bitmap imageview


    【解决方案1】:

    主要是首先你需要在你的图片路径中提到你的图片类型...

    然后您可以使用该图像路径检索:

    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);
    

    你应该添加清单文件的权限:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    

    【讨论】:

    • 没有一个答案有效 :( 还尝试提供图像路径 String img_path ="/storage/sdcard0/Fbt/xylo.jpg"; 仍然没有工作!
    • 你添加权限了吗??
    • 我没有设置读取权限,现在我也设置了,但仍然无法正常工作
    • 我已经更新了问题,我得到了正确的路径为 /storage/sdcard0/Fleet/xylo.jpg 扩展名没有 /mnt/sdcard 根
    【解决方案2】:

    看看this

    您可以使用ImageView.setImageURI(..) 方法。

    preview.setImageURI(Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Echo/Images/"+file_name));
    

    【讨论】:

      【解决方案3】:
      Image paths stored in database with paths without any extensions
      

      您需要在路径中提及图像类型。 e.图片.png

      你可以试试下面的例子。

      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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-22
        • 1970-01-01
        • 2018-04-06
        • 1970-01-01
        • 2013-02-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多