trying to get an image from gallery or camera in a

2020-02-15 09:40发布

I am trying out an online tutorial for loading image in an imageview from either the gallery or camera.The gallery part works fine, but the camera part force closes.The data retrieved from onactvitiyresult shows null.

The code:

package com.example.cameragallerypro;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

     Uri selectedImageUri;
      String  selectedPath;
      ImageView preview;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Button b = (Button) findViewById(R.id.bGallery);
      Button bCam= (Button) findViewById(R.id.bCamera);
       preview = (ImageView) findViewById(R.id.preview);
      bCam.setOnClickListener(new OnClickListener() {

       @Override
       public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(cameraIntent, 100);
       }
      });


      b.setOnClickListener(new OnClickListener() {

       @Override
       public void onClick(View v) {
        // TODO Auto-generated method stub
         openGallery(10);
       }
      });
     }



     public void openGallery(int req_code){

            Intent intent = new Intent();

            intent.setType("image/*");

            intent.setAction(Intent.ACTION_GET_CONTENT);

            startActivityForResult(Intent.createChooser(intent,"Select file to upload "), req_code);

       }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {



            if (resultCode == RESULT_OK) {
             if(data.getData() != null){
               selectedImageUri = data.getData();
             }else{
              Log.d("selectedPath1 : ","Came here its null !");
              Toast.makeText(getApplicationContext(), "failed to get Image!", 500).show();
             }

             if (requestCode == 100 && resultCode == RESULT_OK) { 
                    Bitmap photo = (Bitmap) data.getExtras().get("data");
                    selectedPath = getPath(selectedImageUri);
                    preview.setImageURI(selectedImageUri);
                    Log.d("selectedPath1 : " ,selectedPath);

                }

                if (requestCode == 10)

                {

                   selectedPath = getPath(selectedImageUri);
                   preview.setImageURI(selectedImageUri);
                   Log.d("selectedPath1 : " ,selectedPath);

                }

            }

        }


     public String getPath(Uri uri) {

            String[] projection = { MediaStore.Images.Media.DATA };

            Cursor cursor = managedQuery(uri, projection, null, null, null);

            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

            cursor.moveToFirst();

            return cursor.getString(column_index);

        }

    }

The logcat:

09-17 10:09:30.132: E/AndroidRuntime(7300): FATAL EXCEPTION: main
09-17 10:09:30.132: E/AndroidRuntime(7300): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.cameragallerypro/com.example.cameragallerypro.MainActivity}: java.lang.NullPointerException
09-17 10:09:30.132: E/AndroidRuntime(7300):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
09-17 10:09:30.132: E/AndroidRuntime(7300):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
09-17 10:09:30.132: E/AndroidRuntime(7300):     at android.app.ActivityThread.access$2000(ActivityThread.java:117)
09-17 10:09:30.132: E/AndroidRuntime(7300):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
09-17 10:09:30.132: E/AndroidRuntime(7300):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 10:09:30.132: E/AndroidRuntime(7300):     at android.os.Looper.loop(Looper.java:130)
09-17 10:09:30.132: E/AndroidRuntime(7300):     at android.app.ActivityThread.main(ActivityThread.java:3689)
09-17 10:09:30.132: E/AndroidRuntime(7300):     at java.lang.reflect.Method.invokeNative(Native Method)
09-17 10:09:30.132: E/AndroidRuntime(7300):     at java.lang.reflect.Method.invoke(Method.java:507)
09-17 10:09:30.132: E/AndroidRuntime(7300):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
09-17 10:09:30.132: E/AndroidRuntime(7300):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
09-17 10:09:30.132: E/AndroidRuntime(7300):     at dalvik.system.NativeStart.main(Native Method)
09-17 10:09:30.132: E/AndroidRuntime(7300): Caused by: java.lang.NullPointerException
09-17 10:09:30.132: E/AndroidRuntime(7300):     at android.content.ContentResolver.acquireProvider(ContentResolver.java:743)
09-17 10:09:30.132: E/AndroidRuntime(7300):     at android.content.ContentResolver.query(ContentResolver.java:256)
09-17 10:09:30.132: E/AndroidRuntime(7300):     at android.app.Activity.managedQuery(Activity.java:1550)
09-17 10:09:30.132: E/AndroidRuntime(7300):     at com.example.cameragallerypro.MainActivity.getPath(MainActivity.java:104)
09-17 10:09:30.132: E/AndroidRuntime(7300):     at com.example.cameragallerypro.MainActivity.onActivityResult(MainActivity.java:79)
09-17 10:09:30.132: E/AndroidRuntime(7300):     at android.app.Activity.dispatchActivityResult(Activity.java:3908)
09-17 10:09:30.132: E/AndroidRuntime(7300):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
09-17 10:09:30.132: E/AndroidRuntime(7300):     ... 11 more

How do i modify the code so that the camera part works fine?

6条回答
Root(大扎)
2楼-- · 2020-02-15 10:01
bCam.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                from_camera();

            }
        });

        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                from_gallery();

            }
        });


public void from_camera() {

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

        startActivityForResult(cameraIntent, 111);

    }


public void from_gallery() {
        Intent intent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, 222);


    }





    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub

        if (resultCode == RESULT_OK && requestCode == 111) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            preview.setImageBitmap(photo);



        } else if (resultCode == RESULT_OK && requestCode == 222) {

             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 picturePath = cursor.getString(columnIndex);
            cursor.close();


            preview.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        super.onActivityResult(requestCode, resultCode, data);
    }
查看更多
beautiful°
3楼-- · 2020-02-15 10:06

Try replacing your line:

preview.setImageURI(selectedImageUri);

with this one:

preview.setImageBitmap(photo);
查看更多
够拽才男人
4楼-- · 2020-02-15 10:12

I really advise you to use a library to do the hard work to you. You must take a look on this library, it's much more easy to pick an imagem from gallery or from camera. With the above library an dialog will show up and the user will be able to choose if want get the image from gallery or from camera.

Just call to show the dialog:

PickImageDialog.on(MainActivity.this, new PickSetup());

Make your activity implements this:

public class YourActivity implements IPickResult.IPickResultBitmap  

And then override this method:

@Override
public void onPickImageResult(Bitmap photo) {
     preview.setImageBitmap(photo);
 }
查看更多
【Aperson】
5楼-- · 2020-02-15 10:15

Try this way,hope this will help you to solve your problem.

public class MainActivity extends Activity {

    String  selectedPath;
    ImageView preview;
    final private int GALLERY = 1;
    final private int CAPTURE = 2;
    private String imgPath;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b = (Button) findViewById(R.id.bGallery);
        Button bCam= (Button) findViewById(R.id.bCamera);
        preview = (ImageView) findViewById(R.id.preview);

        bCam.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
                startActivityForResult(intent, CAPTURE);
            }
        });


        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, ""), GALLERY);
            }
        });
    }


    public Uri setImageUri() {
        // Store image in dcim
        File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png");
        Uri imgUri = Uri.fromFile(file);
        this.imgPath = file.getAbsolutePath();
        return imgUri;
    }

    public String getImagePath() {
        return imgPath;
    }


    public String getAbsolutePath(Uri uri) {
        String[] projection = { MediaStore.MediaColumns.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }

    public Bitmap decodeFile(String path) {
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, o);
            // The new size we want to scale to
            final int REQUIRED_SIZE = 70;

            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeFile(path, o2);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;

    }

    public Bitmap decodeFile(Uri path) {
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;

            BitmapFactory.decodeFile(getAbsolutePath(path), o);

            // The new size we want to scale to
            final int REQUIRED_SIZE = 70;

            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeFile(getAbsolutePath(path), o2);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == GALLERY) {
                selectedPath = getAbsolutePath(data.getData());
                preview.setImageBitmap(decodeFile(selectedPath));
            } else if (requestCode == CAPTURE) {
                selectedPath = getImagePath();
                preview.setImageBitmap(decodeFile(selectedPath));
            }
        }

    }
}

Add this permission in AndroidManifest.xml:

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
查看更多
欢心
6楼-- · 2020-02-15 10:16

Please have a look at the following it might help you:

public static String TEMP_PHOTO_FILE_NAME;
private File mFileTemp;
int ADD_IMAGE_CAPTURE = 5;

public void takePictureFromCamera() {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        try {
            Uri mImageCaptureUri = null;
            String state = Environment.getExternalStorageState();
            TEMP_PHOTO_FILE_NAME = "" + System.currentTimeMillis() + ".jpg";
            if (Environment.MEDIA_MOUNTED.equals(state)) {

                mFileTemp = new File(Environment.getExternalStorageDirectory(),
                        TEMP_PHOTO_FILE_NAME);
            } else {
                mFileTemp = new File(getFilesDir(), TEMP_PHOTO_FILE_NAME);
            }
            if (Environment.MEDIA_MOUNTED.equals(state)) {
                mImageCaptureUri = Uri.fromFile(mFileTemp);
            } else {
                /*
                 * The solution is taken from here:
                 * http://stackoverflow.com/questions
                 * /10042695/how-to-get-camera-result-as-a-uri-in-data-folder
                 */
                mImageCaptureUri = InternalStorageContentProvider.CONTENT_URI;
            }
            intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                    mImageCaptureUri);
            intent.putExtra("return-data", true);
            startActivityForResult(intent, ADD_IMAGE_CAPTURE);
        } catch (ActivityNotFoundException e) {

        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
         if (requestCode == ADD_IMAGE_CAPTURE) {
          try {

                if (mFileTemp.exists()) {
                  String mPartyPicUri = mFileTemp.getPath();
                }
             } catch (Exception e) {
                e.printStackTrace();
            }
        }
      }
查看更多
贪生不怕死
7楼-- · 2020-02-15 10:16

Change camera code as below.

 if (requestCode == 100 && resultCode == RESULT_OK) { 
        Bundle extras = data.getExtras();
        if (extras != null) {
            Bitmap photo = extras.getParcelable("data");
             preview.setImageBitmap(photo);

}

查看更多
登录 后发表回答