2017-10-20 24 views
0

스토리지에 이미지를 쓰고 읽는 중입니다. 쓰기 작업은 성공적이지만 읽기 작업은 실패합니다. 책임있는 활동을 호출 할 때 intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);을 사용해 보았지만 도움이되지 않았습니다.파일에 쓰여진 이미지를 읽을 수 없습니다.

MediaStore.Images.Media.getBitmap(context.getContentResolver(), 
       Uri.parse(cursor.getString(cursor.getColumnIndex(eventDBcontract.ListofItem.columnpic)))); 

예외는 =

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

내가 사진`에서 예외를 얻을 :

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 

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

이 나는 ​​이미지를 읽는 방법은 다음과 같습니다 이것은 내가 이미지를 작성하는 방법입니다

나는 체력이있다. 다른 유사한 질문을 cked하지만 그들은 다른 유형의 문제에 모두있는 것처럼 보입니다. 내가 해결하도록 도와 줘.

+0

'Uri photoURI = FileProvid ...'당신은 그 파일을 쓰기 위해 그 URI를 사용하고 있지 않습니다. FileProvider를 사용하여 파일을 쓰지 않습니다. 그래서 주제에서 말하는 것은 사실이 아닙니다. – greenapps

+0

@greenapps 그러면 "fileResolver를 작성하여 파일을 작성해야합니까?" 미안하지만 안드로이드의이 부분은 여전히 ​​나에게 신비입니다. – user5172381

+0

FileOutputStream을 사용하여 파일에 비트 맵을 씁니다. – greenapps

답변

0

photoUri.toString() 대신 데이터베이스에 image.getAbsolutePath()를 넣으십시오.

파일을 읽으려면 데이터베이스에서 경로를 가져옵니다. File 객체를 구축합니다. FileInputStream를 사용합니다. 그런 다음 BitmapFactory가 스트림에서 데이터를 읽도록합니다.

+0

BitmspFactory가 파일에서 읽을 수 있으므로 스트림이 필요하지 않습니다. – greenapps

+0

네, 효과가있었습니다. fileprovider 및 content resolver와 같은 안드로이드에서 이러한 모든 스토리지 관련 내용을 읽을 수있는 장소에 대한 링크를 제공 할 수 있습니까? 지금은 대안을 사용하여 이것을 피하려고합니다. – user5172381

+0

그냥 Google. 나 한테 묻지 마. – greenapps