2017-10-20 20 views
0

イメージをストレージに書き込んで読み込もうとしています。書き込み操作は成功しますが、読み取りは失敗します。私は責任ある活動を呼び出すときに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 

私はcheを持っています他の同様の質問ckedが、彼らはすべての問題の他のタイプのすべてのようだ。私がそれを解決するのを助ける。

+0

'Uri photoURI = FileProvid ...'あなたは何かをファイルに書き込むためにそのURIを使用していません。 FileProviderを使用してファイルを書き込んでいません。それで、あなたが主題で言うことは成就しません。 – greenapps

+0

@greenapps次に、 "fileResolverを書き込むためにcontentResolver"を書く必要があります。申し訳ありませんが、アンドロイドのこの部分はまだ私にとって謎です。 – user5172381

+0

FileOutputStreamを使用してファイルにビットマップを書き込みました。 – greenapps

答えて

0

photoUri.toString()の代わりに、image.getAbsolutePath()をデータベースに配置します。

ファイルを読み取る場合は、データベースからパスを取得します。 Fileオブジェクトを構築します。FileInputStreamを使用します。次に、BitmapFactoryにストリームからデータを読み込ませます。

+0

BitmspFactoryがファイルから読み込めるので、ストリームは必要ありません。 – greenapps

+0

はい、それは働いた。あなたは私がfileproviderやcontent resolverのようなアンドロイドでこれらのストレージ関連のものをすべて読むことができる場所へのリンクを教えてくれますか?今、私はこれを回避するために交互の方法を使っています。 – user5172381

+0

ちょうどGoogle。私に尋ねないでください。 – greenapps

関連する問題