Java Code Examples for com.luck.picture.lib.tools.PictureFileUtils

The following examples show how to use com.luck.picture.lib.tools.PictureFileUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: PictureSelector   Source File: PhotoFragment.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case PictureConfig.APPLY_STORAGE_PERMISSIONS_CODE:
            // 存储权限
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                    PictureFileUtils.deleteCacheDirFile(getContext(), PictureMimeType.ofImage());
                } else {
                    Toast.makeText(getContext(),
                            getString(R.string.picture_jurisdiction), Toast.LENGTH_SHORT).show();
                }
            }
            break;
    }
}
 
Example 2
Source Project: PictureSelector   Source File: MainActivity.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case PictureConfig.APPLY_STORAGE_PERMISSIONS_CODE:
            // 存储权限
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                    PictureFileUtils.deleteCacheDirFile(getContext(), PictureMimeType.ofImage());
                } else {
                    Toast.makeText(MainActivity.this,
                            getString(R.string.picture_jurisdiction), Toast.LENGTH_SHORT).show();
                }
            }
            break;
    }
}
 
Example 3
Source Project: PictureSelector   Source File: PhotoFragmentActivity.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case PictureConfig.APPLY_STORAGE_PERMISSIONS_CODE:
            // 存储权限
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                    PictureFileUtils.deleteCacheDirFile(PhotoFragmentActivity.this, PictureMimeType.ofImage());
                } else {
                    Toast.makeText(PhotoFragmentActivity.this,
                            getString(R.string.picture_jurisdiction), Toast.LENGTH_SHORT).show();
                }
            }
            break;
    }
}
 
Example 4
/**
 * 保存相片至本地相册
 *
 * @throws Exception
 */
private void savePictureAlbum() throws Exception {
    String suffix = PictureMimeType.getLastImgSuffix(mMimeType);
    String state = Environment.getExternalStorageState();
    File rootDir = state.equals(Environment.MEDIA_MOUNTED)
            ? Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
            : getContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    if (rootDir != null && !rootDir.exists() && rootDir.mkdirs()) {
    }
    File folderDir = new File(SdkVersionUtils.checkedAndroid_Q() || !state.equals(Environment.MEDIA_MOUNTED)
            ? rootDir.getAbsolutePath() : rootDir.getAbsolutePath() + File.separator + PictureMimeType.CAMERA + File.separator);
    if (folderDir != null && !folderDir.exists() && folderDir.mkdirs()) {
    }
    String fileName = DateUtils.getCreateFileName("IMG_") + suffix;
    File file = new File(folderDir, fileName);
    PictureFileUtils.copyFile(downloadPath, file.getAbsolutePath());
    onSuccessful(file.getAbsolutePath());
}
 
Example 5
Source Project: PictureSelector   Source File: PictureBaseActivity.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * single crop
 *
 * @param originalPath
 * @param cachePath
 * @param mimeType
 * @param options
 */
private void startSingleCropActivity(String originalPath, String cachePath, String mimeType, UCrop.Options options) {
    boolean isHttp = PictureMimeType.isHasHttp(originalPath);
    String suffix = mimeType.replace("image/", ".");
    File file = new File(PictureFileUtils.getDiskCacheDir(getContext()),
            TextUtils.isEmpty(config.renameCropFileName) ? DateUtils.getCreateFileName("IMG_CROP_") + suffix : config.renameCropFileName);
    Uri uri;
    if (!TextUtils.isEmpty(cachePath)) {
        uri = Uri.fromFile(new File(cachePath));
    } else {
        uri = isHttp || SdkVersionUtils.checkedAndroid_Q() ? Uri.parse(originalPath) : Uri.fromFile(new File(originalPath));
    }
    UCrop.of(uri, Uri.fromFile(file))
            .withOptions(options)
            .startAnimationActivity(this, config.windowAnimationStyle != null
                    ? config.windowAnimationStyle.activityCropEnterAnimation : R.anim.picture_anim_enter);
}
 
Example 6
Source Project: PictureSelector   Source File: PictureBaseActivity.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * startMultipleCropActivity
 *
 * @param cutInfo
 * @param options
 */
private void startMultipleCropActivity(CutInfo cutInfo, int count, UCrop.Options options) {
    String path = cutInfo.getPath();
    String mimeType = cutInfo.getMimeType();
    boolean isHttp = PictureMimeType.isHasHttp(path);
    Uri uri;
    if (!TextUtils.isEmpty(cutInfo.getAndroidQToPath())) {
        uri = Uri.fromFile(new File(cutInfo.getAndroidQToPath()));
    } else {
        uri = isHttp || SdkVersionUtils.checkedAndroid_Q() ? Uri.parse(path) : Uri.fromFile(new File(path));
    }
    String suffix = mimeType.replace("image/", ".");
    File file = new File(PictureFileUtils.getDiskCacheDir(this),
            TextUtils.isEmpty(config.renameCropFileName) ? DateUtils.getCreateFileName("IMG_CROP_")
                    + suffix : config.camera || count == 1 ? config.renameCropFileName : StringUtils.rename(config.renameCropFileName));
    UCrop.of(uri, Uri.fromFile(file))
            .withOptions(options)
            .startAnimationMultipleCropActivity(this, config.windowAnimationStyle != null
                    ? config.windowAnimationStyle.activityCropEnterAnimation : R.anim.picture_anim_enter);
}
 
Example 7
Source Project: DevUtils   Source File: PictureSelectorUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 清空缓存
 * <pre>
 *     包括裁剪和压缩后的缓存, 要在上传成功后调用, 注意: 需要系统 SDCard 权限
 * </pre>
 */
public static void deleteCacheDirFile() {
    try {
        PictureFileUtils.deleteCacheDirFile(DevUtils.getContext());
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "deleteCacheDirFile");
    }
}
 
Example 8
Source Project: PictureSelector   Source File: PhotoFragment.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 清空缓存包括裁剪、压缩、AndroidQToPath所生成的文件,注意调用时机必须是处理完本身的业务逻辑后调用;非强制性
 */
private void clearCache() {
    // 清空图片缓存,包括裁剪、压缩后的图片 注意:必须要在上传完成后调用 必须要获取权限
    if (getContext() != null) {
        if (PermissionChecker.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            //PictureFileUtils.deleteCacheDirFile(this, PictureMimeType.ofImage());
            PictureFileUtils.deleteAllCacheDirFile(getContext());
        } else {
            PermissionChecker.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    PictureConfig.APPLY_STORAGE_PERMISSIONS_CODE);
        }
    }
}
 
Example 9
Source Project: PictureSelector   Source File: MainActivity.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 清空缓存包括裁剪、压缩、AndroidQToPath所生成的文件,注意调用时机必须是处理完本身的业务逻辑后调用;非强制性
 */
private void clearCache() {
    // 清空图片缓存,包括裁剪、压缩后的图片 注意:必须要在上传完成后调用 必须要获取权限
    if (PermissionChecker.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        //PictureFileUtils.deleteCacheDirFile(this, PictureMimeType.ofImage());
        PictureFileUtils.deleteAllCacheDirFile(getContext());
    } else {
        PermissionChecker.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                PictureConfig.APPLY_STORAGE_PERMISSIONS_CODE);
    }
}
 
Example 10
/**
 * 缓存清除
 * 包括裁剪和压缩后的缓存,要在上传成功后调用,注意:需要系统sd卡权限
 */
@ReactMethod
public void deleteCache() {
    Activity currentActivity = getCurrentActivity();
    PictureFileUtils.deleteAllCacheDirFile(currentActivity);
}
 
Example 11
/**
 * 保存图片到picture 目录,Android Q适配,最简单的做法就是保存到公共目录,不用SAF存储
 *
 * @param inputUri
 */
private void savePictureAlbumAndroidQ(Uri inputUri) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, DateUtils.getCreateFileName("IMG_"));
    contentValues.put(MediaStore.Images.Media.DATE_TAKEN, ValueOf.toString(System.currentTimeMillis()));
    contentValues.put(MediaStore.Images.Media.MIME_TYPE, mMimeType);
    contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, PictureMimeType.DCIM);
    Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
    if (uri == null) {
        ToastUtils.s(getContext(), getString(R.string.picture_save_error));
        return;
    }
    PictureThreadUtils.executeByIo(new PictureThreadUtils.SimpleTask<String>() {

        @Override
        public String doInBackground() {
            BufferedSource buffer = null;
            try {
                buffer = Okio.buffer(Okio.source(Objects.requireNonNull(getContentResolver().openInputStream(inputUri))));
                OutputStream outputStream = getContentResolver().openOutputStream(uri);
                boolean bufferCopy = PictureFileUtils.bufferCopy(buffer, outputStream);
                if (bufferCopy) {
                    return PictureFileUtils.getPath(getContext(), uri);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (buffer != null && buffer.isOpen()) {
                    PictureFileUtils.close(buffer);
                }
            }
            return "";
        }

        @Override
        public void onSuccess(String result) {
            PictureThreadUtils.cancel(PictureThreadUtils.getIoPool());
            onSuccessful(result);
        }
    });
}
 
Example 12
public String showLoadingImage(String urlPath) {
    Uri outImageUri = null;
    OutputStream outputStream = null;
    InputStream inputStream = null;
    BufferedSource inBuffer = null;
    try {
        if (SdkVersionUtils.checkedAndroid_Q()) {
            outImageUri = createOutImageUri();
        } else {
            String suffix = PictureMimeType.getLastImgSuffix(mMimeType);
            String state = Environment.getExternalStorageState();
            File rootDir =
                    state.equals(Environment.MEDIA_MOUNTED)
                            ? Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
                            : getContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
            if (rootDir != null) {
                if (!rootDir.exists()) {
                    rootDir.mkdirs();
                }
                File folderDir = new File(!state.equals(Environment.MEDIA_MOUNTED)
                        ? rootDir.getAbsolutePath() : rootDir.getAbsolutePath() + File.separator + PictureMimeType.CAMERA + File.separator);
                if (!folderDir.exists() && folderDir.mkdirs()) {
                }
                String fileName = DateUtils.getCreateFileName("IMG_") + suffix;
                File file = new File(folderDir, fileName);
                outImageUri = Uri.fromFile(file);
            }
        }
        if (outImageUri != null) {
            outputStream = Objects.requireNonNull(getContentResolver().openOutputStream(outImageUri));
            URL u = new URL(urlPath);
            inputStream = u.openStream();
            inBuffer = Okio.buffer(Okio.source(inputStream));
            boolean bufferCopy = PictureFileUtils.bufferCopy(inBuffer, outputStream);
            if (bufferCopy) {
                return PictureFileUtils.getPath(this, outImageUri);
            }
        }
    } catch (Exception e) {
        if (outImageUri != null && SdkVersionUtils.checkedAndroid_Q()) {
            getContentResolver().delete(outImageUri, null, null);
        }
    } finally {
        PictureFileUtils.close(inputStream);
        PictureFileUtils.close(outputStream);
        PictureFileUtils.close(inBuffer);
    }
    return null;
}
 
Example 13
Source Project: PictureSelector   Source File: PictureBaseActivity.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * start to camera、preview、crop
 */
protected void startOpenCamera() {
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (cameraIntent.resolveActivity(getPackageManager()) != null) {
        Uri imageUri;
        if (SdkVersionUtils.checkedAndroid_Q()) {
            imageUri = MediaUtils.createImageUri(getApplicationContext(), config.suffixType);
            if (imageUri != null) {
                config.cameraPath = imageUri.toString();
            } else {
                ToastUtils.s(getContext(), "open is camera error,the uri is empty ");
                if (config.camera) {
                    closeActivity();
                }
                return;
            }
        } else {
            int chooseMode = config.chooseMode == PictureConfig.TYPE_ALL ? PictureConfig.TYPE_IMAGE
                    : config.chooseMode;
            String cameraFileName = "";
            if (!TextUtils.isEmpty(config.cameraFileName)) {
                boolean isSuffixOfImage = PictureMimeType.isSuffixOfImage(config.cameraFileName);
                config.cameraFileName = !isSuffixOfImage ? StringUtils.renameSuffix(config.cameraFileName, PictureMimeType.JPEG) : config.cameraFileName;
                cameraFileName = config.camera ? config.cameraFileName : StringUtils.rename(config.cameraFileName);
            }

            File cameraFile = PictureFileUtils.createCameraFile(getApplicationContext(),
                    chooseMode, cameraFileName, config.suffixType, config.outPutCameraPath);
            if (cameraFile != null) {
                config.cameraPath = cameraFile.getAbsolutePath();
                imageUri = PictureFileUtils.parUri(this, cameraFile);
            } else {
                ToastUtils.s(getContext(), "open is camera error,the uri is empty ");
                if (config.camera) {
                    closeActivity();
                }
                return;
            }
        }
        config.cameraMimeType = PictureMimeType.ofImage();
        if (config.isCameraAroundState) {
            cameraIntent.putExtra(PictureConfig.CAMERA_FACING, PictureConfig.CAMERA_BEFORE);
        }
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        startActivityForResult(cameraIntent, PictureConfig.REQUEST_CAMERA);
    }
}
 
Example 14
Source Project: PictureSelector   Source File: PictureBaseActivity.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * start to camera、video
 */
protected void startOpenCameraVideo() {
    Intent cameraIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    if (cameraIntent.resolveActivity(getPackageManager()) != null) {
        Uri videoUri;
        if (SdkVersionUtils.checkedAndroid_Q()) {
            videoUri = MediaUtils.createVideoUri(getApplicationContext(), config.suffixType);
            if (videoUri != null) {
                config.cameraPath = videoUri.toString();
            } else {
                ToastUtils.s(getContext(), "open is camera error,the uri is empty ");
                if (config.camera) {
                    closeActivity();
                }
                return;
            }
        } else {
            int chooseMode = config.chooseMode ==
                    PictureConfig.TYPE_ALL ? PictureConfig.TYPE_VIDEO : config.chooseMode;
            String cameraFileName = "";
            if (!TextUtils.isEmpty(config.cameraFileName)) {
                boolean isSuffixOfImage = PictureMimeType.isSuffixOfImage(config.cameraFileName);
                config.cameraFileName = isSuffixOfImage ? StringUtils.renameSuffix(config.cameraFileName, PictureMimeType.MP4) : config.cameraFileName;
                cameraFileName = config.camera ? config.cameraFileName : StringUtils.rename(config.cameraFileName);
            }
            File cameraFile = PictureFileUtils.createCameraFile(getApplicationContext(),
                    chooseMode, cameraFileName, config.suffixType, config.outPutCameraPath);
            if (cameraFile != null) {
                config.cameraPath = cameraFile.getAbsolutePath();
                videoUri = PictureFileUtils.parUri(this, cameraFile);
            } else {
                ToastUtils.s(getContext(), "open is camera error,the uri is empty ");
                if (config.camera) {
                    closeActivity();
                }
                return;
            }
        }
        config.cameraMimeType = PictureMimeType.ofVideo();
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
        if (config.isCameraAroundState) {
            cameraIntent.putExtra(PictureConfig.CAMERA_FACING, PictureConfig.CAMERA_BEFORE);
        }
        cameraIntent.putExtra(PictureConfig.EXTRA_QUICK_CAPTURE, config.isQuickCapture);
        cameraIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, config.recordVideoSecond);
        cameraIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, config.videoQuality);
        startActivityForResult(cameraIntent, PictureConfig.REQUEST_CAMERA);
    }
}