Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以只将图像保存在Android应用程序私有目录中,而不是设备的内部或任何外部存储?_Android_Android Storage - Fatal编程技术网

是否可以只将图像保存在Android应用程序私有目录中,而不是设备的内部或任何外部存储?

是否可以只将图像保存在Android应用程序私有目录中,而不是设备的内部或任何外部存储?,android,android-storage,Android,Android Storage,我不想将我的Android应用程序图像存储在内部或外部设备存储上,以便它对多媒体资料可见 现在,我正在将我的应用程序图像保存在外部设备存储器上。但是我想将图像保存到应用程序私有目录中。我怎样才能做到这一点 这是我的活动代码: public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data

我不想将我的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);
                }

            }
        }
    }
保存图像方法

 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();
    }
}
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;
} 
getPathFromUri方法

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

你在找什么?那你想把它保存在哪里。。你是说在网络上吗?阅读。为便于搜索,请不要添加带疑问的IDE标签,除非问题与IDE无关,即
android studio
不在内部设备存储上不在外部设备存储上
,那么您应该将图像保存在云存储上。@RashidKalhoro您有3个选项。数据库、本地存储或云。您可以将图像保存在作为内部存储的应用缓存目录中。阅读