澄清在android中保存和检索图像

时间:2017-02-21 07:51:38

标签: android

我是android编程的新手。我的目标是保存捕获的图像并在用户需要时检索它。我遇到了我的项目的这个问题。 问题是如果我没有在Android手机中使用外部存储卡,如何编写代码。我搜索了一些网站并写了这样的东西。我不知道这是否正确。我希望我能得到帮助。

public void takephoto1(){ 
        Intent i=new Intent();
        i.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        File dir= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

        output=new File(dir, "firstphoto.jpg");
        i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
        startActivityForResult(i,q);

    }


    public void onActivityResult(int requestcode ,int resultcode,Intent data){
     if(requestcode==q && resultcode==RESULT_OK){
         Intent i=new Intent(Intent.ACTION_VIEW);
         i.setDataAndType(Uri.fromFile(output), "image/jpeg");
         Bundle extras = data.getExtras();
         Bitmap  photo = (Bitmap) extras.get("data");
         image.setImageBitmap(photo);
         startActivity(i);
         finish();
     }

    }

2 个答案:

答案 0 :(得分:0)

就这样做

 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
 startActivityForResult(cameraIntent,2);

然后

     @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       Bitmap photo = (Bitmap) data.getExtras().get("data");
       ivCrop.setImageBitmap(scaledBitmap);
}

答案 1 :(得分:0)

 Intent cameraIntent = new  Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    //cameraIntent.setAction(Intent.ACTION_GET_CONTENT);
    File file = new File(Environment.getExternalStorageDirectory(),
            "MyPhoto.jpg");
    outPutfileUri = Uri.fromFile(file);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
    startActivityForResult(cameraIntent, 2);

   and in onActivityRewsult use

                   File imgFile = new  File(Environment.getExternalStorageDirectory(),"MyPhoto.jpg");
                Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                ivCrop.setImageBitmap(myBitmap);