【问题标题】:How to save a picture in an application?如何在应用程序中保存图片?
【发布时间】:2019-07-26 14:29:05
【问题描述】:

我想知道如何将图像保存在我的存储中,因为在我的应用程序中我可以拍照并查看它,但是当我关闭我的应用程序并重新打开它时,图像消失了我使用了位图。

我的代码:

public static final int REQUEST_CODE = 01;
private ImageView imgs1;
private File outputMediaFile;

protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.semana1);
     imgs1 = (ImageView) findViewById(R.id.imgs1);
}

public void btnTakePhotoClicked1(View v){
    Intent intent1= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent1, REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK){
        Bitmap bm1= (Bitmap) data.getExtras().get("data");
        imgs1.setImageBitmap(bm1);
    }
} 

【问题讨论】:

  • 您只想将它​​们保存在图库或应用中?
  • 我只希望图像不要消失。
  • @spain 请不要玷污你自己的问题。
  • 将该位图路径保存在 SharedPreference 中,并在再次打开应用程序时检查它并使用该路径再次加载

标签: java android image save


【解决方案1】:

在获得图像位图对象后,在onActivityResult中使用此代码。

File file = new File("foldername/imagename.jpg")
OutputStream fOut = new FileOutputStream(file);
bm1.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 2013-05-24
    相关资源
    最近更新 更多