Pergunta

I'm using these codes to capture and store an image on a button click.

takePhoto.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        destination = new File(Environment
                .getExternalStorageDirectory(), 
                preferences.getSelectedItem().getItemNo() + "_" 
                + preferences.getSelectedItem().getChasisNo() + "_up");
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(destination));
        startActivityForResult(intent, TAKE_PHOTO_CODE);
    }
});

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
        FileInputStream in;
        try {
            in = new FileInputStream(destination);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 10;
            imgPath = destination.getAbsolutePath();
            // Bitmap bmp = BitmapFactory.decodeStream(in, null, options);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.d("Image saved", "Image saved at " + imgPath);
        upItem.setUpPhotoURL(String.valueOf(imgPath));
        isPhotoAttached = true;
    }
}

But when I capture the photo and try to confirm it, app does nothing. Cancelling and retaking options work perfectly but confirming the taken image doesn't do anything. Can anyone point me where the problem lies here?

Foi útil?

Solução 3

I think this is not working because the name of the image is too big. When I changed this

destination = new File(Environment.getExternalStorageDirectory(), 
                preferences.getSelectedItem().getItemNo() + "_" 
                + preferences.getSelectedItem().getChasisNo() + "_up");

to this

destination = new File(Environment.getExternalStorageDirectory(), 
                preferences.getSelectedItem().getChasisNo() + ".jpg");

I'm not sure if there's such a thing as the image name being too big, but this change is working for me

Outras dicas

added this permission in your manifest file

<uses-feature android:name="android.hardware.camera" /> 

.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
    String[] projection = { MediaStore.Images.Media.DATA}; 
    @SuppressWarnings("deprecation")
    Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null); 
    int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst();
    String capturedImageFilePath = cursor.getString(column_index_data);

Toast.makeText(this, capturedImageFilePath, Toast.LENGTH_SHORT).show();

}  

Try this

String mCurrentPhotoPath = destination.getAbsolutePath();

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
    case TAKE_PHOTO_CODE: {
        if (resultCode == RESULT_OK) {

            if (mCurrentPhotoPath != null) {
                getBitmap(mCurrentPhotoPath);
            }
        }
        break;
    }

    default:

        break;
    }

}

private Bitmap getBitmap(String mCurrentPhotoPath) {

    /* There isn't enough memory to open up more than a couple camera photos */
    /* So pre-scale the target bitmap into which the file is decoded */

    /* Get the size of the ImageView */
    int targetW = imageView.getMeasuredWidth();
    int targetH = imageView.getMeasuredHeight();

    /* Get the size of the image */
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    Bitmap oribitmap = BitmapFactory.decodeFile(mCurrentPhotoPath,
            bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    /* Figure out which way needs to be reduced less */
    int scaleFactor = 3;

    /* Set bitmap options to scale the image decode target */
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    /* Decode the JPEG file into a Bitmap */
    return oribitmap = BitmapFactory.decodeFile(mCurrentPhotoPath,
            bmOptions);

}

This permission in your manifest file

<uses-feature android:name="android.hardware.camera" /> 
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top