2017-09-26 230 views
-1

可能重複但未找到任何解決方案。MediaStore.Images.Media.insertImage在某些設備中返回null

我想修改圖像並使用MediaStore將其保存到設備。用下面的代碼 - :

public class Utils { 

    private Utils() { 

    } 

    static Uri getUri(Context context, Bitmap bitmap) { 
     ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
     String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, "Title", null); //returning null in some devices 
     Log.d("TAG", "getUri: "+path); 
     return Uri.parse(path); //this is returning null which cause the app crash 
    } 

    static Bitmap getBitmap(Context context, Uri uri) throws IOException { 
     return MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri); 
    } 
} 

此代碼是在大多數設備的工作很好,但問題是,在MediaStore.Images.Media.insertImage像一些設備返回null - :

。三星J7(2016)

。 LG Magna LTE

。 Android模擬器API 21

我很驚訝這個應用程序工作在Android模擬器API 26。但得到崩潰Android模擬器API 21

我已經搜索谷歌,我也發現了同樣的問題here但沒有找到任何足夠的答案。

注 - :我已經使用運行權限讀/寫外部存儲

但在提到的設備仍然得到崩潰。下面是我的崩潰對數:

Fatal Exception: java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:0, request=1, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:51 flg=0x1 }} to activity {com.example/com.scanlibrary.ScanActivity}: java.lang.NullPointerException: uriString 
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3574) 
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617) 
    at android.app.ActivityThread.access$1300(ActivityThread.java:151) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5254) 
    at java.lang.reflect.Method.invoke(Method.java) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Caused by java.lang.NullPointerException: uriString 
     at android.net.Uri$StringUri.<init>(Uri.java:470) 
     at android.net.Uri$StringUri.<init>(Uri.java:460) 
     at android.net.Uri.parse(Uri.java:432) 
     at com.scanlibrary.Utils.getUri(Utils.java:24) 
     at com.scanlibrary.PickImageFragment.postImagePick(PickImageFragment.java:228) 
     at com.scanlibrary.PickImageFragment.onActivityResult(PickImageFragment.java:208) 
     at android.app.Activity.dispatchActivityResult(Activity.java:6196) 
     at android.app.ActivityThread.deliverResults(ActivityThread.java:3570) 
     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617) 
     at android.app.ActivityThread.access$1300(ActivityThread.java:151) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5254) 
     at java.lang.reflect.Method.invoke(Method.java) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

感謝

+0

在'Bitmap'上使用'compress()'將圖像寫入到您所選文件的'FileOutputStream'中。使用「MediaScannerConnection」索引。作爲獎勵功能,您現在可以知道圖像的存儲位置,並可以選擇用戶可能會欣賞的地方。您當前的代碼將圖像存儲在您或用戶選擇的位置。 – CommonsWare

+0

我同意@CommonsWare一個很好的解決方案來保存文件,或者我也使用'FileOutputStream'將pdf文件保存到我的應用程序中的特定位置。但我想知道爲什麼會發生這種情況。 – UltimateDevil

+0

顯然,圖像未能被存儲。在「MediaStore」標籤中檢查LogCat中的消息(例如,「無法插入圖像」,「無法創建縮略圖,刪除原始文件」)。 – CommonsWare

回答

2

而不是使用下面的代碼 - :

String path = MediaStore.Media.insertImage(context.getContentResolver(),bitmap,"Title",null); 

使用此代碼(下) - :

ContentValues values=new ContentValues(); 
values.put(MediaStore.Images.Media.TITLE,"Title"); 
values.put(MediaStore.Images.Media.DESCRIPTION,"From Camera"); 
Uri path=getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values); 

傳遞值如果它不工作,則爲null。

+0

感謝您的答案@Jithesh這適用於保存圖像數據庫的罰款,但是當我從'相同'uri' gettingBitmap'然後我得到'java.io.FileNotFoundException:沒有這樣的文件或目錄。 – UltimateDevil

+0

無論上面的代碼,我補充說,適用於拍攝從相機拍攝的圖像。 –

+0

好的謝謝你的回答 – UltimateDevil

0
protected void onActivityResult(int requestCode, int resultCode,Intent data) { 

if (requestCode == RequestPermissionCode && resultCode == RESULT_OK) { 

     try { 

      String[] filePathColumn = {MediaStore.Images.Media.DATA}; 

      Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, filePathColumn, null, null, null); 
      if (cursor == null) 
       return; 
      // find the file in the media area 
      cursor.moveToLast(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      String filePath = "file:///"+cursor.getString(columnIndex); 
      cursor.close(); 
      actualImage = FileUtil.from(this,Uri.parse(filePath)); 
      actualImageView.setImageBitmap(BitmapFactory.decodeFile(actualImage.getAbsolutePath())); 

     } catch (Exception e) { 
      Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

使用onActvittyResult方法,我希望它能工作。

相關問題