act*_*ram 2 android uri image bitmap save
我正在尝试从现有URI创建一个位图,旋转位图并将其保存到与JPEG文件相同的位置.在尝试了几个解决方案之后,这是我当前的代码:
try {
// Get the Bitmap from the known URI. This seems to work.
Bitmap bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), this.currUserImgUri);
// Rotate the Bitmap thanks to a rotated matrix. This seems to work.
Matrix matrix = new Matrix();
matrix.postRotate(-90);
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
// Create an output stream which will write the Bitmap bytes to the file located at the URI path.
File imageFile = new File(this.currUserImgUri.getPath());
FileOutputStream fOut = new FileOutputStream(imageFile); // --> here an Exception is catched; see below.
// The following doesn't work neither:
// FileOutputStream fOut = new FileOutputStream(this.currUserImgUri.getPath());
// Write the compressed file into the output stream
bmp.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
被捕获的例外情况如下:
java.io.FileNotFoundException:/ external/images/media/8439:open failed:ENOENT(没有这样的文件或目录)
任何人都可以向我解释如果我刚刚创建文件并且可以访问其URI,文件是否不存在?
也许我对此一切都错了?在这种情况下,根据其URI将旋转图像保存到同一位置的正确方法是什么?
嘿,你可以这样做使用将位图写入文件.
// Rotate the Bitmap thanks to a rotated matrix. This seems to work.
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), photoURI);
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap bmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
//learn content provider for more info
OutputStream os=getContext().getContentResolver().openOutputStream(photoURI);
bmp.compress(Bitmap.CompressFormat.PNG,100,os);
Run Code Online (Sandbox Code Playgroud)
不要忘记刷新和关闭输出流.实际上内容提供商拥有自己的uri计划.
| 归档时间: |
|
| 查看次数: |
3248 次 |
| 最近记录: |