1

我一直试图在捕获图像数据后将其传递回应用程序。但是,它在尝试返回我的应用程序时总是崩溃。

启动意图的代码是:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imgView = (ImageView) findViewById(R.id.ImageView);
    upload = (Button) findViewById(R.id.Upload);
    snap = (Button) findViewById(R.id.Snap);
    select = (Button) findViewById(R.id.Select);
    subject = (EditText) findViewById(R.id.Subject);
    msg = (EditText) findViewById(R.id.Message);snap.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub
             String imageFilePath = Environment.getExternalStorageDirectory().
             getAbsolutePath() + "/picture.jpg";  
             File imageFile = new File(imageFilePath); 
             Uri imageFileUri = Uri.fromFile(imageFile); // convert path to Uri

             Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
             it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri); 
             startActivityForResult(it, CAMERA_RESULT);
            }
        });
}

接收意图的代码是:

 case CAMERA_RESULT:
        if (resultCode == Activity.RESULT_OK) {

                    // Get Extra from the intent 
                    Bundle extras = data.getExtras(); 
                    // Get the returned image from extra 
                    Bitmap bmp = (Bitmap) extras.get("data"); 

                    imgView = (ImageView) findViewById(R.id.ImageView); 
                    imgView.setImageBitmap(bmp); 
              }
        break;

发生应用程序崩溃时,我还会收到以下异常:

java.lang.RuntimeException:无法恢复活动 {com.NYP.estatemanagement/com.NYP.estatemanagement.MainActivity}:java.lang.RuntimeException:失败交付结果 ResultInfo{who=null,request=100,result=-1, data=null} 到活动 {com.NYP.estatemanagement/com.NYP.estatemanagement.MainActivity}:java.lang.NullPointerException

4

2 回答 2

2

试试这对我来说就像魅力一样

private String selectedImagePath = "";
    final private int PICK_IMAGE = 1;
    final private int CAPTURE_IMAGE = 2;

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

btnGallery.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, ""), PICK_IMAGE);

            }
        });

        btnCapture.setOnClickListener(new OnClickListener() {

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

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_CANCELED) {
            if (requestCode == PICK_IMAGE) {
                selectedImagePath = getAbsolutePath(data.getData());
                imgUser.setImageBitmap(decodeFile(selectedImagePath));
            } else if (requestCode == CAPTURE_IMAGE) {
                selectedImagePath = getImagePath();
                imgUser.setImageBitmap(decodeFile(selectedImagePath));
            } else {
                super.onActivityResult(requestCode, resultCode, data);
            }
        }

    }


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 String getAbsolutePath(Uri uri) {
        String[] projection = { MediaColumns.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }
于 2013-08-30T05:51:55.267 回答
0

这个(下)来自@Biraj Zalavadia(答案):

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

String selectedImagePath = getAbsolutePath(data.getData());

我只需要

于 2014-01-07T04:32:38.947 回答