小米系統(tǒng)相冊是真的坑

系統(tǒng)相冊做兼容性測試的時候,遇到神坑手機Redmi Note4。

先上調(diào)用的代碼:

//調(diào)用相冊
 private void go2Gallery() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        try {
            startActivityForResult(intent, PICK_IMAGE_DATA);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }

在其他手機都能正確的打開相冊,但是在這個手機會出現(xiàn)下面這個截圖

image.png

然后點擊相冊,沒有任何反應,看了log也沒有任何的報錯,然后就開始各種百度使用了下面的調(diào)用方式:

//調(diào)用相冊
 private void go2Gallery() {
        Intent intent = new Intent();
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("image/*");
        if (Build.VERSION.SDK_INT < 19) {
            intent.setAction(Intent.ACTION_GET_CONTENT);
        } else {
            intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
        }
        try {
            startActivityForResult(intent, PICK_IMAGE_DATA);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }

懷著即將解決的心情,打開測試手機,尼瑪這次直接打開的是個樹形的文件管理器。內(nèi)心奔潰中,差點懷疑人生。然后對比了前面的兩個方法,無非就是intent的action不一樣,然后就接著百度終于還有一個action叫做Intent.ACTION_PICK。然后換了下面的代碼:

//調(diào)用相冊
  private void go2Gallery() {
        Intent intent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        try {
            startActivityForResult(intent, PICK_IMAGE_DATA);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }

哎呀媽呀,終于成了,然后又試了試其它的手機,包括6.0,7.0的Android手機,都正常。問題總算是解決了。

下面是完整的代碼,僅供參考~

//調(diào)用系統(tǒng)相機
 private void doTakePhoto() {
        mCurrentPhotoFile = new File(yourpath);
        if (!mCurrentPhotoFile.exists()) {
            try {
                mCurrentPhotoFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(mCurrentPhotoFile));
        try {
            startActivityForResult(intent, CAMERA_WITH_DATA);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }
    
    //調(diào)用系統(tǒng)相冊
    private void go2Gallery() {
        Intent intent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        try {
            startActivityForResult(intent, PICK_IMAGE_DATA);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }
    
    
      @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
        if (requestCode == CAMERA_WITH_DATA) {// 從拍照界面回來
               Uri uri = data.getData();
                if (uri != null) {
                String path = Util.getGoogleImagePath(mActivity, uri); 
                //...do your code
                    
                }
            } else if (requestCode == PICK_IMAGE_DATA) {// 從相冊選擇圖片回來
                Uri uri = data.getData();
                if (uri != null) {
                    String path = Util.getGoogleImagePath(mActivity, uri); 
                    //...do your code
                }
            }
        }
    }
    
    private  String getGoogleImagePath(Context context, Uri uri) {
        File cacheDir = null;
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
            cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), ".OCFL311");
        } else {
            cacheDir = context.getCacheDir();
        }
        if (!cacheDir.exists()) {
            cacheDir.mkdirs();
        }
        File file = new File(cacheDir, "store_image.jpg");
        try {
            InputStream is = context.getContentResolver().openInputStream(uri);
            if (is.available() == 0)
                return null;
            OutputStream os = new FileOutputStream(file);
            byte[] buffer = new byte[1024];
            int length = -1;
            while ((length = is.read(buffer)) != -1) {
                os.write(buffer, 0, length);
            }
            is.close();
            os.close();
            return file.getAbsolutePath();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容