我试图捕获图像然后为了良好的练习我将位图文件放在FileOutputStream
我如何使用文件名恢复位图。
活动
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK){
if (requestCode == REQUEST_CODE_CAMERA && data != null){
fragment = null;
Bitmap bmp = (Bitmap) data.getExtras().get("data");
String filename = "bitmap.png";
try {
FileOutputStream stream = this.openFileOutput(filename, Context.MODE_PRIVATE);
assert bmp != null;
bmp.compress(Bitmap.CompressFormat.PNG,100,stream);
stream.close();
bmp.recycle();
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in_left,R.anim.fade_out,R.anim.fade_in,R.anim.slide_out_right);
fragment =FragmentCropper.newInstance(filename);
fragmentTransaction.add(R.id.fragmentContainer,fragment,Constant.BackStackTag.CROP_TAG);
fragmentTransaction.addToBackStack(Constant.BackStackTag.CROP_TAG);
fragmentTransaction.commit();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
在片段中
如何使用文件名
恢复位图 try {
//FileInputStream stream = getActivity().openFileInput(filename);
//mBitmap = BitmapFactory.decodeStream(stream);
//stream.close();
String path = "path/"+filename;
mBitmap = BitmapUtils.decodeSampledBitmapFromFile(path,500,500);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (mBitmap.isRecycled())
mBitmap.recycle();
mBitmap=null;
}
答案 0 :(得分:0)
String path = "path/"+filename;
更改为
String path = getFilesDir().getAbsolutePath() + "/" + filename;