簡體   English   中英

如何在應用程序中保存圖片?

[英]How to save a picture in an application?

我想知道如何將圖像保存在存儲器中,因為在我的應用程序中我可以拍照並看到它,但是當我關閉應用程序並重新打開它時,該圖像消失了,我使用了位圖。

我的代碼:

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);
    }
} 

獲取圖像位圖對象后,請在onActivityResult中使用此代碼。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM