Можно ли сохранять изображения только в личном каталоге приложения Android, а не во внутреннем или внешнем хранилище устройства? - PullRequest
0 голосов
/ 26 декабря 2018

Я не хочу хранить изображения моего приложения Android во внутреннем или внешнем хранилище устройства, чтобы оно стало видимым для галереи.

В данный момент я сохраняю изображения приложения в хранилище внешнего устройства.Но я хочу сохранить изображения в личном каталоге приложения.Как мне этого добиться?

Вот моя активность код:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK) {

            if (requestCode == REQUEST_CAMERA) {

                Uri selectedImageUri = data.getData();

                if (null != selectedImageUri) {
                    // Get the path from the Uri

                    String path = getPathFromURI(selectedImageUri);
                    File file = new File(path);
                    Bitmap bmp = CommonMethod.compressImage(file, getContext());
                    Log.e(TAG, "onActivityResult --: " + String.format("Size : %s", getReadableFileSize(file.length())));

                    mCustomerImage = CommonMethod.bitmapToByteArray(bmp);
                    imageTemplateStr = Base64.encodeToString(mCustomerImage, Base64.DEFAULT);
                    Log.e(TAG, "image: " + imageTemplateStr);
                    //CommonMethod.SaveImage(bmp);
                    imageCustomer.setImageBitmap(bmp);

                    CommonMethod.SaveImage(bmp);
                }

            } else if (requestCode == SELECT_FILE) {
                Uri selectedImageUri = data.getData();
                if (null != selectedImageUri) {
                    // Get the path from the Uri

                    String path = getPathFromURI(selectedImageUri);
                    File file = new File(path);
                    Bitmap bmp = CommonMethod.compressImage(file, getContext());
                    Log.e(TAG, "onActivityResult --: " + String.format("Size : %s", getReadableFileSize(file.length())));

                    mCustomerImage = CommonMethod.bitmapToByteArray(bmp);
                    imageTemplateStr = Base64.encodeToString(mCustomerImage, Base64.DEFAULT);
                    //CommonMethod.SaveImage(bmp);
                    Log.e(TAG, "image: " + imageTemplateStr);

                    imageCustomer.setImageBitmap(bmp);

                    CommonMethod.SaveImage(bmp);
                }

            }
        }
    }

SaveImage Method

 public static void SaveImage(Bitmap finalBitmap) {

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/.safco_private_pics");
    if (!myDir.exists()) {
        myDir.mkdirs();
    }
    File newFile = new File(root,".nomedia");
    try {
        FileWriter writer = new FileWriter(newFile);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-"+ n +".jpg";
    File file = new File (myDir, fname);
    if (file.exists ())
        file.delete ();
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

Метод getPathFromUri

public String getPathFromURI(Uri contentUri) {
    String res = null;
    String[] proj = {MediaStore.Images.Media.DATA};
    Cursor cursor = getActivity().getContentResolver().query(contentUri, proj, null, null, null);
    if (cursor.moveToFirst()) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        res = cursor.getString(column_index);
    }
    cursor.close();
    return res;
} 
...