天天看點

調用相冊與相機擷取--路徑

直接開說,先檢查權限,然後啟動彈框

//檢查權限問題
    private void inspectPermission() {
        // 如果SDK版本大于或等于23才需要檢查權限,否則直接撥彈出圖庫
        if (Build.VERSION.SDK_INT >= ) {
            // 檢查權限是否允許
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
                Log.e("TAG", "沒有權限");
                // 沒有權限,考慮是否需要解釋為什麼需要這個權限
                /*申請權限的解釋,該方法在使用者上一次已經拒絕過你的這個權限申請時使用。
                * 也就是說,使用者已經拒絕一次了,你又彈個授權框,你需要給使用者一個解釋,為什麼要授權*/
                if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    Log.e("TAG", "沒有權限,使用者上次已經拒絕該權限,解釋為什麼需要這個權限");
                    // Show an expanation to the user *asynchronously* -- don't block this thread
                    //waiting for the user's response! After the user
                    // sees the explanation, try again to request the permission.
                    Toast.makeText(this, "需要權限才能上傳圖檔哦", Toast.LENGTH_SHORT).show();

                } else {
                    Log.e("TAG", "沒有權限,申請權限");
                    // 申請權限
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CALL_PHONE);
                }
            } else {
                Log.e("ydp", "有權限,執行相關操作");
                // 有權限,執行相關操作
                toPic();
            }
            //當是6.0以下版本時直接執行彈出拍照圖庫視窗
        } else {
            toPic();
        }
    }
           
//去圖庫的方法
    private void toPic() {
        // 寫一個去圖庫選圖的Intent
        Intent intent1 = new Intent(Intent.ACTION_PICK);
        intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        // 寫一個打開系統拍照程式的intent
        Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), System.currentTimeMillis() + ".jpg");
        cameraPath = file.getAbsolutePath();
        photoUri = Uri.fromFile(file);
        intent2.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
        // Intent選擇器
        Intent intent = Intent.createChooser(intent1, "選擇頭像...");
        intent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                new Intent[]{intent2});
        startActivityForResult(intent, );
    }
           

這裡是不同意權限的時候的回調

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CALL_PHONE) {
            if (grantResults.length >  && grantResults[] == PackageManager.PERMISSION_GRANTED) {
                Log.e(TAG, "權限同意");
                toPic();
            } else {
                Log.e(TAG, "權限拒絕");
                Toast.makeText(this, "權限拒絕", Toast.LENGTH_SHORT).show();
            }
            return;
        }
    }
           

接下來是回調了或去路徑

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   //外界的程式通路ContentProvider所提供資料 可以通過ContentResolver接口
                ContentResolver resolver = getContentResolver();
//          //此處的用于判斷接收的Activity是不是你想要的那個
                // 作為頭像的圖檔在裝置上的本地路徑是什麼(/sdcard/XXX/XXXX.jpg)
                String filePath = "";
                if (data != null) {
                    // 圖檔是使用者從圖庫選擇得到的
                    // uri代表使用者選取的圖檔在MediaStroe中存儲的位置
                    Uri uri = data.getData();
                    // 利用ContentResolver查找uri所對應圖檔在裝置上的真實路徑
                    Cursor cursor = getContentResolver().query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null);
                    cursor.moveToNext();
                    filePath = cursor.getString();
                    Log.i("hxl", "filePath========" + filePath);
                    //顯得到bitmap圖檔
                    Bitmap bm = null;
                    try {
                        bm = MediaStore.Images.Media.getBitmap(resolver, uri);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    imgFileName = filePath.substring(filePath.lastIndexOf("/") + );
                    Log.i("hxl", "imgFileName========" + imgFileName);
                    File file = new File(filePath);
                    Log.e("ydp", "img========================================" + filePath);
                    Map<String, Object> hashMap = new HashMap<>();
                    hashMap.put("seq", EMClient.getInstance().getCurrentUser());
                    post_file("http://192.168.1.180:8082/rest/user/fileUpload", hashMap, file, bm);
                    cursor.close();
                } else {
              ///  這是相機擷取的路勁的代碼
              ///這的在源碼有這裡我就補貼出來了
                }
}
           

這是源碼位址 Demo