是否只能将图片保存在Android应用私有目录中,而不是保存在设备的内部或外部存储中?

时间:2018-12-26 10:51:51

标签: android android-storage

我不想将我的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方法

 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;
} 

0 个答案:

没有答案