无法读取写入文件的图像

问题描述:

我正尝试在存储上写入图像然后读取它。写入操作成功,但读取失败。我在尝试使用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); 
} 

我在'照片获得异常=

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 

我有车提出了其他类似的问题,但似乎他们都是为了一些其他类型的问题。帮我解决它。

+0

'Uri photoURI = FileProvid ...'你没有使用该URI写入文件。您没有使用FileProvider来编写文件。所以你在这个问题上说什么不成立。 – greenapps

+0

@greenapps然后我应该写“contentResolver写入文件”。我很抱歉,但Android的这部分对我来说仍然是一个谜。 – user5172381

+0

您使用FileOutputStream将位图写入文件。 – greenapps

而不是photoUri.toString()把image.getAbsolutePath()放在你的数据库中。

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

+0

您不需要流,因为BitmspFactory可以从文件读取。 – greenapps

+0

是的,它工作。你能提供给我的链接到我可以在android中读取所有这些与存储相关的东西的地方,比如fileprovider和content resolver吗?现在,我尝试通过使用替代方法来避免这种情况。 – user5172381

+0

就是谷歌。不要问我。 – greenapps