【问题标题】:Android ImageView show image by pathAndroid ImageView 按路径显示图片
【发布时间】:2017-07-15 16:07:51
【问题描述】:

我正在制作一个在 SQLite 中存储图像路径的应用程序。然后我们打开新的活动,它应该显示来自数据库的图像。数据库工作正常,我测试了我是否得到结果(路径)并且它工作(用 toast 测试,见下文),但我不能让它在 ImageView 中显示图像,它总是空白。这是我的一些代码:

XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/slikatest"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

</LinearLayout>

这是显示布局的活动类:

public class SimpleViewExample extends AppCompatActivity {

DBAdapter db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pogled);

    db = new DBAdapter(SimpleViewExample.this);

    db.openDB();

    Cursor c = db.getOneImage(1);
    c.moveToFirst();

    Toast.makeText(getApplicationContext(), c.getString(2), Toast.LENGTH_LONG).show();

    ImageView img = (ImageView) findViewById(R.id.slikatest);
    Bitmap bitmap = BitmapFactory.decodeFile(c.getString(2));

    img.setImageBitmap(bitmap);

   }
}

Toast 返回结果如下:/storage/emulated/0/Pictures/Screenshots/example.png

欢迎任何帮助。提前谢谢!

更新:

看起来我的权限有问题,在 logcat 中发现了这个错误: open failed: EACCES (Permission denied).

知道如何解决这个问题吗?

修复:

我太笨了,对不起。我只需要进入设置 -> 应用程序 -> 我的应用程序 -> 允许使用内存。

对不起,玩得开心

【问题讨论】:

  • 为什么需要在数据库中存储图片路径?您想缓存您的图片以加快加载速度吗?

标签: java android sqlite imageview


【解决方案1】:

在 android manifest 中包含权限

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

正如你所说,你的c.getString(2) 返回图片路径。

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pogled);

    db = new DBAdapter(SimpleViewExample.this);

    db.openDB();

    Cursor c = db.getOneImage(1);
    c.moveToFirst();

    Toast.makeText(getApplicationContext(), c.getString(2), Toast.LENGTH_LONG).show();


File imagefile = new  File(c.getString(2));

if(imagefile.exists()){

    Bitmap imageBitmap = BitmapFactory.decodeFile(imagefile.getAbsolutePath());

    ImageView img = (ImageView) findViewById(R.id.slikatest);

    img.setImageBitmap(imageBitmap);

}
}

【讨论】:

  • if(imagefile.exists()) ,这看起来很方便,谢谢。 +1。
  • 如果这是解决方案,请将其标记为已回答。谢谢。
【解决方案2】:

试试这个

img.setBackgroundResource(位图);

而不是

img.setImageBitmap(位图);

【讨论】:

    【解决方案3】:

    只需在设置 -> 应用程序 -> 中提供权限,然后选择我已安装的应用程序 -> 权限 -> 允许使用内存。

    【讨论】:

      【解决方案4】:

      如果您在 Android 6.0+ 上进行测试,您必须进一步管理运行时权限,以便如上所述在清单中添加权限。

      Android runtime permissions guidance

      下面是对我有用的代码(Android 7.0):

      1. 在将路径作为字符串存储在数据库中后(在我的情况下使用 ContentValues),我在自定义 CursorAdapter 类中检索,bindView 方法使用:

        ImageView prodImageView = (ImageView) view.findViewById(R.id.prod_img);
        
        int picColumnIndex = cursor.getColumnIndex(InventoryContract.ProductEntry.COLUMN_PRODUCT_PIC);
        
        
        String prodPathPic = cursor.getString(picColumnIndex);
        
        File f = new File(prodPathPic);
        
        Bitmap imgBmp = BitmapFactory.decodeFile(f.getAbsolutePath());
        prodImageView.setImageBitmap(imgBmp);
        
        }
        
        1. 为了使其正常工作,我在主要活动的 onCreate() 方法中添加了以下代码,以询问用户对内部存储器的访问权限:

      .

      int permissionCheck = ContextCompat.checkSelfPermission(this,
                  Manifest.permission.WRITE_EXTERNAL_STORAGE);
          if (ContextCompat.checkSelfPermission(this,
                  Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
      
              // Should we show an explanation?
              if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                      Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
      
                  // Show an explanation to the user *asynchronously* -- don't block
                  // this thread waiting for the user's response! After the user
                  // sees the explanation, try again to request the permission.
      
              } else {
      
                  // No explanation needed, we can request the permission.
      
                  ActivityCompat.requestPermissions(this,
                          new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                          1);
      
                  // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                  // app-defined int constant. The callback method gets the
                  // result of the request.
              }
          }
      

      希望对你有所帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-15
        • 2018-06-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多