2
我正在嘗試爲我的應用實現一項功能,該功能允許用戶從圖庫中爲某些提案選擇圖片。我需要在應用更改(過濾器,裁剪等)之前將此圖片保存爲新圖片。Android將圖片從圖庫複製到新文件
到目前爲止,我所做的:
private void pickImageFromGallery(){
/*Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, GALLERY_SELECT_PICTURE);*/
Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
getIntent.setType("image/*");
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(getIntent, "");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent});
startActivityForResult(chooserIntent, GALLERY_SELECT_PICTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
if(requestCode == GALLERY_SELECT_PICTURE){
if(data == null){
//TODO SHOW ERROR
return;
}
try {
Bitmap temporaryBitmap = MediaStore.Images.Media.getBitmap(myContext.getContentResolver(), data.getData());
//Tried using inputStream and got the same result
//InputStream inputStream = myContext.getContentResolver().openInputStream(data.getData());
//Bitmap temporaryBitmap = BitmapFactory.decodeStream(inputStream, null, options);
//Just return a file to save the bitmap into (I use the same code in different activities and it works perfectly)
capturedImage = FunctionUtil.getOutputMediaFile(ConstUtil.ids.MEDIA_TYPE_IMAGE);
FileOutputStream outputStream = new FileOutputStream(capturedImage);;
temporaryBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
//Just refresh the gallery so my new picture becomes available (I use the same function in different activities and it works fine)
FunctionUtil.refreshMediaGallery(capturedImage);
//HERE I CHECK THE PATH/GALLERY AND NOTICE THE FILE ISN'T SAVED
} catch (Exception e) {
e.printStackTrace();
//TODO SHOW ERROR
}
}else if(requestCode == GALLERY_SELECT_VIDEO){
}
}
}
的代碼來獲得一個新的文件(FunctionUtil.getOutputMediaFile),代碼以刷新畫廊(FunctionUtil.refreshMediaGallery)和代碼來保存位圖(Bitmap.compress)在同一活動的不同部分可以正常工作,但帶有來自圖庫的圖片它只是不能保存它們!
當我使用相機API拍攝新照片然後解碼爲位圖時,它完美地工作,但當我從照片庫中選取圖片並解碼爲位圖時,它無法正常工作。