簡體   English   中英

MediaStore.Images.Media.insertImage在某些設備中返回null

[英]MediaStore.Images.Media.insertImage returns null in some devices

可能重復,但未找到任何解決方案。

我正在嘗試修改圖像,並使用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:

1 三星J7(2016)

2 LG Magna LTE

3 Android模擬器API 21

我很驚訝這個應用程序可以在Android Emulator API 26中運行 但是在Android Emulator API 21上崩潰

我已經搜索過Google,並且在這里也找到了相同的問題但是找不到足夠的答案。

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

但是在提到的設備上仍然崩潰。 以下是我的崩潰日志:

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)

謝謝

而不是使用下面的代碼:

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。

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方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM