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

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

}

}

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值