Issue in capture image using camera in lollipop

2019-08-27 19:17发布

问题:

I have been searching for the following issue for three days

My Code is as following

private void openCamera () {    
    try {
        Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        String fileName = Constants.IMAGE_CAPTURE_FILE_NAME_PREFIX + System.currentTimeMillis() + Constants.IMAGE_FILE_EXT_JPG;
        mImageFile = new File(FileManager.getInstance().getFileFullName(Constants.IMAGE_FOLDER, fileName));
        captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageFile);
        startActivityForResult(captureIntent, Constants.ACTIVITY_CAMERA_REQUEST_CODE);     
    } catch(ActivityNotFoundException anfe) {   
        String errorMessage = "Whoops - your device doesn't support capturing images!";
        Toast toast = Toast.makeText(ImagesSelectorActivity.this, errorMessage, Toast.LENGTH_SHORT);
        toast.show();
    }
}



public String getFileFullName(String folderName, String fileName) {
    String filepath;
    if (PhoneUtils.isSDCardFound() == true)
        filepath = Environment.getExternalStorageDirectory().getPath();
    else
        filepath = Environment.getDataDirectory().getPath();

    File file = new File(filepath, folderName);

    if (!file.exists()) {
        file.mkdirs();
    }

    return (file.getAbsolutePath() + "/" + fileName);
}

On Activity results

if(requestCode == Constants.ACTIVITY_CAMERA_REQUEST_CODE) {

    Uri picUri = data.getData();

    if (picUri == null) {
        Bitmap bitmap = (Bitmap) data.getExtras().get("data");
        Uri tempUri = PhoneUtils.getBitmapUri(getApplicationContext(), bitmap);
        Log.i("AMIRA", tempUri.toString());

    }

    performPreview(picUri);
}

The problem that the image is not saved in my folder, and the url that return is the thumbnail uri

I need to save the image after capture in the path that I need and to be able to read the full size of image.

I can't find the image captured from camera in any place in the device.

回答1:

I use almost same process as yours that works fine. Please try doing the following:

In the open camera part, instead of sending File send Uri like this:

mImageUri = Uri.fromFile(mImageFile); //keep mImageUri as member so that you can use it on the activity result.
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);  

Afterwards in onActivityResult just do this:

if (mImageUri != null && !mImageUri.getPath().isEmpty()) {
    performPreview(mImageUri);
}

Hope this helps!



回答2:

Class file as follows :

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MyCameraActivity extends Activity {
private static final int CAMERA_REQUEST = 1888; 
private ImageView imageView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.imageView = (ImageView)this.findViewById(R.id.imageView1);
    Button photoButton = (Button) this.findViewById(R.id.button1);
    photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(cameraIntent, CAMERA_REQUEST); 
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
    }  
} 

}

XML file :

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >

 <Button android:id="@+id/button1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Take Picture">
 </Button>

<ImageView 
android:id="@+id/imageView1"   
android:layout_height="wrap_content" 
android:src="@drawable/ic_launcher" 
android:layout_width="wrap_content">
</ImageView>

Add Permission in Manifest file :

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

Hope this helps...