簡體   English   中英

無法從android 7.1及更高版本的設備中從圖庫中選擇圖像

[英]Unable to pick the image from the gallery from android 7.1 and above devices

我正在嘗試從圖庫中選擇圖像,它在android 7.1以下運行良好,但是從android 7.1開始,它不適用於我。 如何解決相同的問題。 這是我嘗試的代碼:

public void LaunchGalleryView() {
        mFileUri = null;
        Intent intent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intent.setType("image/*");
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_CODE_GALLERY);
    }

這是onActivityResult()中的代碼:

 case REQUEST_CODE_GALLERY:
                if (data == null) {
                    return;
                }
                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                File file = new File(picturePath);
                mFileUri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", file);
                performCrop();
                break;

如何解決相同,請幫助我。

這是錯誤日志:

12-28 05:13:30.085 13533-13533 /? E / AndroidRuntime:致命例外:主進程:com.welldoc.platform.android.cyan,PID:13533 java.lang.RuntimeException:無法交付結果ResultInfo {who = null,request = 1,result = -1,data = Intent {dat = content://com.google.android.apps.photos.contentprovider/0/1/mediakey:/ local%3Aefc38517-5fd9-4b57-afbf-6329e508fb66 / ORIGINAL / NONE / 1646850308 flg = 0x1 clip = {text / uri-list U:content://com.google.android.apps.photos.contentprovider/0/1/mediakey%3A%2Flocal%253Aefc38517-5fd9-4b57-afbf-6329e508fb66/ORIGINAL/NONE/1646850308}}}到活動{com.welldoc.platform.android.cyan / com.welldoc.platform.android.ui.profile.ProfileDetailsEditActivity}的活動:android.app.ActivityThread.deliverResults(ActivityThread.java:4324)上的java.lang.NullPointerException .app.ActivityThread.handleSendResult(ActivityThread.java:4367)位於android.app.ActivityThread.wrap19(未知來源:0)位於android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1649)位於android.os。 Handler.dispatchMessage(Handler.jav a:105),位於android.os.Looper.loop(Looper.java:164),位於com。的java.lang.reflect.Method.invoke(Native Method),位於android.app.ActivityThread.main(ActivityThread.java:6541) .android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)由以下原因引起:java.io上的java.lang.NullPointerException android.app.Activity.dispatchActivityResult(Activity.java:7235)上的com.welldoc.platform.android.ui.profile.ProfileDetailsEditActivity.onActivityResult(ProfileDetailsEditActivity.java:1059)上的.File。(File.java:282) .app.ActivityThread.deliverResults(ActivityThread.java:4320)在android.app.ActivityThread.handleSendResult(ActivityThread.java:4367)在android.app.ActivityThread.-wrap19(未知來源:0)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1649)在android.os.Handler.dispatchMessage(Handler.java:105)在android.os.Looper.loop(Looper.java:164)在android.app.ActivityThread.main(ActivityThread的.java:654 1)在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)在java.lang.reflect.Method.invoke(本機方法)在com.android.internal.os.ZygoteInit.main( ZygoteInit.java:767)

1.將圖片路徑與其他手機圖片選擇的路徑進行比較,有時路徑格式可能會有所不同,因此您可以得到空值。

方法 FileProvider.getUriForFile()的第二個參數要求您創建自定義FileProvider並在應用程序的清單中對其進行定義:

授權字符串:在應用清單中的元素中定義的FileProvider的授權。

如果只想獲取文件的Uri ,則可以使用以下代碼:

Uri.fromFile(file);

我正在嘗試從圖庫中選擇圖像,並且在android 7.1以下的版本中工作正常

僅在嘗試的設備上針對您嘗試的方案。 在許多其他設備上,以及在其他情況下,它將失敗:

  • ACTION_PICK不需要從MediaStore返回Uri
  • MediaStore不需要給您一個路徑
  • MediaStore不需要為您提供可讀取的路徑,更不用說寫入

這是onActivityResult()中的代碼:

您已經有一個Uri 您不需要FileProvider 您不需要為此查詢MediaStore selectedImageUri 因此,將其與首選的圖像裁剪庫一起使用 ,並使該庫將其結果保存到您控制的某個文件中。

如果出於某種原因,您一開始需要文件:

  • 調用getContentResolver()以獲取ContentResolver
  • ContentResolver上調用openInputStream() ,傳入selectedImage ,以獲取該內容上的InputStream
  • 在您控制的文件上創建FileOutputStream (例如,在getCacheDir()
  • 將數據從InputStream復制到FileOutputStream
  • 使用結果文件

嘗試這個

  • 您必須從代碼中刪除此行,只需添加以下startActivityForResult(intent,1);
  • 我認為您傳遞了錯誤的意圖 ,這往往不會打開gallery而是打開DocumentProvider
  • 把它放在你的OnActivityResult

     @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) { try { uri = null; uri = data.getData(); Bitmap bmp = null; file_galleryimagepath = null; bmp = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri); File f = new File(getActivity().getCacheDir(), System.currentTimeMillis() + ".jpg"); if (f.exists()) f.delete(); f.createNewFile(); Bitmap bitmap = bmp; ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos); byte[] bitmapdata = bos.toByteArray(); FileOutputStream fos = new FileOutputStream(f); fos.write(bitmapdata); fos.flush(); fos.close(); file_galleryimagepath = f; Bitmap map = BitmapFactory.decodeFile(file_galleryimagepath.toString()); iv_userImage.setImageBitmap(map); }catch (Exception e){ Log.e("204","HFP>>>"+e+"<<catch"); } } } } 

  • 當您嘗試從DocumentProvider' its result becomes獲取file pathDocumentProvider' its result becomes崩潰`,因此您必須像這樣傳遞意圖。

暫無
暫無

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

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