无法读取写入文件的图像

时间:2017-10-20 13:35:37

标签: android android-fileprovider

我正在尝试在存储上写入图像然后阅读它。写操作成功但读取失败。我在调用负责任的活动时尝试使用intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);但没有帮助。 这就是我写图像的方式:

public void savepic(Bitmap pic)
{
    SQLiteDatabase db = dBcontract.getWritableDatabase();
    ContentValues values = new ContentValues();

    FileOutputStream out = null;
    try
    {
        File image = createImageFile();
        Uri photoURI = FileProvider.getUriForFile(context, "lcukerd.com.android.fileprovider", image);
        out = new FileOutputStream(image);
        pic.compress(Bitmap.CompressFormat.PNG, 100, out);
        values.put(eventDBcontract.ListofItem.columnpic, photoURI.toString());
        db.insert(eventDBcontract.ListofItem.tableName2, null, values);
        Log.i(TAG, "Pic saved " + photoURI.toString());
    } catch (Exception e)
    {
        e.printStackTrace();
    } finally
    {
        try
        {
            if (out != null)
            {
                out.close();
            }
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

private File createImageFile() throws IOException
{
    String EName = "Image";
    File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(EName, ".jpg", storageDir);
    return image;
}

这就是我读图片的方式:

public Bitmap getDownloadedpics(int index)
{
    SQLiteDatabase db = dBcontract.getReadableDatabase();
    Cursor cursor = db.query(eventDBcontract.ListofItem.tableName2, projection2, null, null, null, null, null);
    cursor.moveToPosition(index);
    Bitmap photo = null;
    try
    {
        Log.d(TAG,cursor.getString(cursor.getColumnIndex(eventDBcontract.ListofItem.columnpic)));
        photo = MediaStore.Images.Media.getBitmap(context.getContentResolver(),
                Uri.parse(cursor.getString(cursor.getColumnIndex(eventDBcontract.ListofItem.columnpic))));
    } catch (IOException e)
    {
        Log.e(TAG, "Can't read image");
    }
    Log.d(TAG, "Returned " + String.valueOf(cursor.getCount()) + " pics");
    return (photo);
}

我在`photo =

得到了例外
MediaStore.Images.Media.getBitmap(context.getContentResolver(),
                Uri.parse(cursor.getString(cursor.getColumnIndex(eventDBcontract.ListofItem.columnpic))));

例外是:

java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{6118b35 21772:lcukerd.com.instaswipe/u0a157} (pid=21772, uid=10157) that is not exported from uid 10101

我已经检查了其他类似的问题,但似乎它们都是针对其他类型的问题。帮我解决一下。

1 个答案:

答案 0 :(得分:0)

而不是photoUri.toString()将image.getAbsolutePath()放入数据库。

如果要读取文件,请从数据库中获取路径。使用FileInputStream构造File对象。然后让BitmapFactory从流中读取数据。