有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java安卓:从intent获取“null”

  • 我正在开发一个应用程序,在这个应用程序中,我想从相机捕获图像,并在图像视图的另一个活动中显示它,我的问题是,在OnActivityResult中,我得到的数据为“null”。。。我不知道为什么

这是我的密码。。 PictureOptions.java

public void buttonCameraOpen(View view)
{
    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

private static Uri getOutputMediaFileUri(int type){
      return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "Easy Heal");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

 // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     Bitmap selectedphoto   = null;

     super.onActivityResult(requestCode, resultCode, data);

     if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE  && resultCode == RESULT_OK && null!=data) {

            // Image captured and saved to fileUri specified in the Intent
            //selectedphoto = (Bitmap) data.getExtras().get("data");
            Uri selectedImage = data.getData();
             String [] filePathColumn = {MediaStore.Images.Media.DATA};
             Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
             cursor.moveToFirst();   
             int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
             String filePath = cursor.getString(columnIndex);
             File f =new File(filePath);
             String filename = f.getName();
             cursor.close();
             selectedphoto = BitmapFactory.decodeFile(filePath);

             Intent intent = new Intent(PictureOptions.this,ShowImage.class);
             //intent.putExtra("data", selectedphoto);
             intent.setData( selectedImage );
             startActivity(intent);
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
       }

}

PictureOptions.xml

<Button
    安卓:id="@+id/buttonCameraOpen"
    安卓:layout_width="fill_parent"
    安卓:layout_height="72dp"
    安卓:layout_weight="0.35"
    安卓:onClick="buttonCameraOpen"
    安卓:text="@string/button_camera_open" />

ShowImage.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_image);
    ImageView imageview = (ImageView)findViewById(R.id.ImageShow);
    Uri imageUri = getIntent().getData();
    //Bitmap selectedphoto  =(Bitmap)this.getIntent().getParcelableExtra("data");
    imageview.setImageURI(imageUri);
}

ShowImage.xml

 <ImageView
    安卓:id="@+id/ImageShow"
    安卓:layout_width="fill_parent"
    安卓:layout_height="fill_parent" />

共 (3) 个答案

  1. # 1 楼答案

    从捕获图像开始,您不需要将此图像存储在文件中。您可以使用以下方法直接获取位图。这只是你在做一些额外的工作

    //Global variable
    private static final int TAKE_IMAGE = 2;
    
    
    //OnClick of your button or whereever you want to start camera
       @Override
            public void onClick(View v) {
    
    
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
                startActivityForResult(cameraIntent, TAKE_IMAGE); 
           }
    
    //onActivityREsul portion of your code
    

    @覆盖 ActivityResult上受保护的void(int请求代码、int结果代码、意图数据){

        if(requestCode == TAKE_IMAGE ){
            try {
    
                    bitmap = (Bitmap) data.getExtras().get("data");
               // bitmap = MediaStore.Images.Media.getBitmap( getApplicationContext().getContentResolver(),  capturedImageUri);
    
    
                YourImageViewObject.setImageBitmap(bitmap);
                } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                } 
    
    
        }
    
    
        super.onActivityResult(requestCode, resultCode, data);
    }
    
  2. # 2 楼答案

    如果未添加此代码,请在活动中也使用此代码

    @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
    
            fileUri = savedInstanceState.getParcelable("file_uri");
    
            super.onRestoreInstanceState(savedInstanceState);
        }
    
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            // TODO Auto-generated method stub
    
            outState.putParcelable("file_uri", fileUri);
            super.onSaveInstanceState(outState);
        }
    
  3. # 3 楼答案

    This happened because you forgot to add Write External storage permission Or the folder you trying to save image doest not exist , please create folder first.

    请添加android.permission.WRITE_EXTERNAL_STORAGE

    尝试保存另一个位置

    File mediaStorageDir = new File(Environment.getExternalStorageDirectory()+"/"+"Easy Heal");