Android无需权限保存文件,即使使用用户权限,也无法在外部存储上保存文件[Android]...

我正在为

Android上的图像处理开发一个应用程序,但我仍然在编写用于保存图像的代码.

这是我使用的方法:

private void saveImageToExternalStorage(Bitmap finalBitmap) {

String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();

File myDir = new File(root + "/saved_images");

myDir.mkdirs();

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

}

// Tell the media scanner about the new file so that it is

// immediately available to the user.

MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null,

new MediaScannerConnection.OnScanCompletedListener() {

public void onScanCompleted(String path, Uri uri) {

Log.i("ExternalStorage", "Scanned " + path + ":");

Log.i("ExternalStorage", "-> uri=" + uri);

}

});

}

每次执行此操作时,FileOutputStream都会给出FileNotFoundException.

我的清单:

package="com.example.gabrielerestuccia.testimmagini">

...

我正在Linux上的Android Studio 1.5.1上开发这个,使用内置的虚拟Nexus 5和Android 6.0.

谢谢您的帮助.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值