【问题标题】:Set Image using getAbsolutePath()使用 getAbsolutePath() 设置图像
【发布时间】:2023-03-03 12:23:01
【问题描述】:

我正在尝试使用自定义图库并尝试获取所选图像并希望在列表视图中显示它

这就是我想要做的:

if (extras != null) {
        for (int i = 0; i < fetchList.size(); i++) {
            Bitmap originBitmap = null;
            filepath = fetchList.get(i);
            newStringList.add(filepath);

            imgfilename = filepath.substring(filepath.lastIndexOf("/") + 1);
            Uri selectedImage = Uri.fromFile(new File(filepath));
            File myFile = new File(selectedImage.getPath());
            myFile.getAbsolutePath();
            Toast.makeText(MainActivity.this,myFile.getAbsolutePath(),Toast.LENGTH_LONG).show();
            listimage.setImageBitmap(myFile.getAbsolutePath());
            myStringList.add(imgfilename);
            arrayAdapter = new ArrayAdapter<String>(
                    this,
                    R.layout.custom_textview, R.id.listtext,
                    myStringList);
            lv.setAdapter(arrayAdapter);
            arrayAdapter.notifyDataSetChanged();
        }
 }

现在我的问题是如何使用此路径将图像设置为图像视图? 如果我用这个listimage.setImageBitmap(myFile.getAbsolutePath()); 我看不到图片。

我得到的路径是:/storage/emulated/0/

【问题讨论】:

    标签: android path imageview


    【解决方案1】:

    试试这个

    private AQuery aq = new AQuery(activity);
    
    File imgFile = new File(filepath);
    aq.id(imageView).image(imgFile, false, 300, new BitmapAjaxCallback() {
                            @Override
                            public void callback(String url, ImageView iv, Bitmap bm, AjaxStatus status) {
                                iv.setImageBitmap(bm);
                            }
                        });
    

    【讨论】:

      【解决方案2】:

      请尝试这种方式,

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

      希望这会对你有所帮助。

      【讨论】:

      • 没有应用程序因收到此错误而崩溃尝试在空对象引用上调用虚拟方法 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)'
      • 这意味着您正在尝试在图像视图中设置空位图。从您的代码中,我可以看到您将路径作为 /storage/emulated/0/ 而它应该是 /storage/emulated/0/。正如您在我的回答中看到的那样,它具有直到 image.jpg 文件的完整路径。
      猜你喜欢
      • 2020-01-23
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-30
      • 2018-10-16
      相关资源
      最近更新 更多