我正在尝试从图库中获取图像,并将其保存到应用程序中的私人位置以供其他使用。我可以确认是否从图库中收到了位图(可以在活动中设置图像视图),但是在其他活动中无法访问该图像。我相信我的FileOutputStream做错了。有什么想法吗?
Uri photoURI = data.getData();
Bitmap imageBitmap;
//Full Size
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
try {
imageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoURI);
bitmapOptions.inJustDecodeBounds = true;
//Compress
ByteArrayOutputStream photoStream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 50,photoStream);
picPreview.setImageBitmap(imageBitmap);
//Save to DB
FileOutputStream fileOutputStream = new FileOutputStream(photoFile);
fileOutputStream.write(photoStream.toByteArray());
fileOutputStream.flush();
fileOutputStream.close();
}catch (IOException ex) {
// Error occurred while creating the File
Toast.makeText(
getApplicationContext(),
R.string.error_photo_file,
Toast.LENGTH_LONG).show();
}
这是调用意图并创建“ PhotoFile”的函数。
private void ChoosePictureIntent(){
try {
photoFile = createImageFile();
//Save path
wordPhotoDIR = photoFile.getAbsolutePath();
thumbnailFile = createImageFile();
//Save path
ThumbnailDIR = thumbnailFile.getAbsolutePath();
} catch (IOException ex) {
// Error occurred while creating the File
Toast.makeText(
getApplicationContext(),
R.string.error_photo_file,
Toast.LENGTH_LONG).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.roomwordsample.fileprovider",
photoFile);
Intent ChoosePictureIntent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(ChoosePictureIntent, REQUEST_IMAGE_CHOOSE);
}
}