Saving image taken from camera into INTERNAL storage

前端 未结 1 1222
我寻月下人不归
我寻月下人不归 2020-12-06 04:03

I just got started in Android Programming and I am creating an android application in which the user is allowed to key in information, take a picture with ImageView implemen

相关标签:
1条回答
  • 2020-12-06 04:16

    getOutputMediaFile() is returning a directory, not a file. You need to specify a path to a (not-yet-existing) file where you want the image to be written.

    This is illustrated in this activity from this sample application:

    package com.commonsware.android.camcon;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.provider.MediaStore;
    import java.io.File;
    
    public class CameraContentDemoActivity extends Activity {
      private static final int CONTENT_REQUEST=1337;
      private File output=null;
    
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File dir=
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    
        output=new File(dir, "CameraContentDemo.jpeg");
        i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
    
        startActivityForResult(i, CONTENT_REQUEST);
      }
    
      @Override
      protected void onActivityResult(int requestCode, int resultCode,
                                      Intent data) {
        if (requestCode == CONTENT_REQUEST) {
          if (resultCode == RESULT_OK) {
            Intent i=new Intent(Intent.ACTION_VIEW);
    
            i.setDataAndType(Uri.fromFile(output), "image/jpeg");
            startActivity(i);
            finish();
          }
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题