从片段中的FileOutputStream恢复位图文件

问题描述 投票:1回答:1

我试图捕获图像然后为了良好的实践我把位图文件放在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;
    }
android fileinputstream fileoutputstream
1个回答
0
投票
 String path = "path/"+filename;

改成

String path = getFilesDir().getAbsolutePath() + "/" + filename;
© www.soinside.com 2019 - 2024. All rights reserved.