iamutkarshtiwari / Ananas

An easy image editor integration for your Android apps.
MIT License
250 stars 112 forks source link

Failed To Load The Image! Android Q #75

Closed HritikBhat closed 3 years ago

HritikBhat commented 3 years ago

Greetings, The library is fabulously working till API 28 or Android Pie. But when I run it in Android Q or API 29(or above version). It fails to load the image. Then I decided to look upon the code and I saw the library using BitmapFactory.decodeFile which I think would be difficult to use as Android Q or 10 has curbed Storage Path for User privacy. From my view, these functions are making trouble for Android 10

utils/BitmapUtils.java

For loading the image: public static Bitmap getSampledBitmap(String filePath, int reqWidth, int reqHeight) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inPreferredConfig = Bitmap.Config.RGB_565; options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(filePath, options); } For saving the image public static boolean saveBitmap(Bitmap bm, String filePath) { File f = new File(filePath); if (f.exists()) { f.delete(); } try { FileOutputStream out = new FileOutputStream(f); bm.compress(Bitmap.CompressFormat.PNG, 90, out); out.flush(); out.close(); return true; } catch (FileNotFoundException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } }

Putting some reference which might help the library: For saving an image in Android 10: https://stackoverflow.com/a/56990305

Plus, putting android:requestLegacyExternalStorage="true" in Manifest won't be a good idea as Google would enforce in Android R or Android 11.

Please update these code to get compatible with Android 10 or above. Thank You

ravi-savaj commented 3 years ago

File outputFile = FileUtils.genEditFile(); try { Intent intent = new ImageEditorIntentBuilder(this, root, outputFile.getAbsolutePath()) .withAddText() // Add the features you need .withPaintFeature() .withFilterFeature() .withRotateFeature() .withCropFeature() .withBrightnessFeature() .withSaturationFeature() .withBeautyFeature() .withStickerFeature() .forcePortrait(true) // Add this to force portrait mode (It's set to false by default) .setSupportActionBarVisibility(false) // To hide app's default action bar .build(); EditImageActivity.start(ImagePriview.this, intent, PHOTO_EDITOR_REQUEST_CODE); } catch (Exception e) { Log.e("Demo App", e.getMessage()); // This could throw if either sourcePath or outputPath is blank or Null }

than cretate FileUtils Class:

public class FileUtils { public static final String FOLDER_NAME = "Destination File Name";

/**
 * 获取存贮文件的文件夹路径
 *
 * @return
 */
public static File createFolders() {

File baseDir; if (android.os.Build.VERSION.SDK_INT < 8) { baseDir = Environment.getExternalStorageDirectory(); } else { baseDir = Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); } if (baseDir == null) return Environment.getExternalStorageDirectory(); File aviaryFolder = new File(baseDir, FOLDER_NAME); if (aviaryFolder.exists()) return aviaryFolder; if (aviaryFolder.isFile()) aviaryFolder.delete(); if (aviaryFolder.mkdirs()) return aviaryFolder; return Environment.getExternalStorageDirectory(); }

public static File genEditFile(){
    return FileUtils.getEmptyFile("FolderName"
            + System.currentTimeMillis() + ".jpg");
}

public static File getEmptyFile(String name) {
    File folder = FileUtils.createFolders();
    if (folder != null) {
        if (folder.exists()) {
            File file = new File(folder, name);
            return file;
        }
    }
    return null;
}

/**
 * 删除指定文件
 *
 * @param path
 * @return
 */
public static boolean deleteFileNoThrow(String path) {
    File file;
    try {
        file = new File(path);
    } catch (NullPointerException e) {
        return false;
    }

    if (file.exists()) {
        return file.delete();
    }
    return false;
}

/**
 * 保存图片
 *
 * @param bitName
 * @param mBitmap
 */
public static String saveBitmap(String bitName, Bitmap mBitmap) {
    File baseFolder = createFolders();
    File f = new File(baseFolder.getAbsolutePath(), bitName);
    FileOutputStream fOut = null;
    try {
        f.createNewFile();
        fOut = new FileOutputStream(f);
    } catch (IOException e) {
        e.printStackTrace();
    }
    mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    try {
        fOut.flush();
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return f.getAbsolutePath();
}

// 获取文件夹大小
public static long getFolderSize(File file) throws Exception {
    long size = 0;
    try {
        File[] fileList = file.listFiles();
        for (int i = 0; i < fileList.length; i++) { // 如果下面还有文件
            if (fileList[i].isDirectory()) {
                size = size + getFolderSize(fileList[i]);
            } else {
                size = size + fileList[i].length();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return size;
}

/** * 格式化单位 * * @param size * @return */
public static String getFormatSize(double size) {
    double kiloByte = size / 1024d;
    int megaByte = (int) (kiloByte / 1024d);
    return megaByte + "MB";
}

/**
 *
 * @Description:
 * @Author 11120500
 * @Date 2013-4-25
 */
public static boolean isConnect(Context context) {
    try {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    } catch (Exception e) {

    }
    return false;
}

}

fileutility.zip

iamutkarshtiwari commented 3 years ago

Closing since it's similar to this issue which is resolved in version 1.2.5.