简体   繁体   中英

Image loses it's original result when passing it to another activity

I am working on a camera feature in my application. I am capturing an image and passing it to another activity. The problem that I'm facing is when I display the image in another activity it loses its original result (gets pixilated) for some reason. This is how I'm doing it:

private void takePhotoFromCamera() {
        if(ActivityCompat.checkSelfPermission(EnterDataView.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }else {
            String[] permissionRequest = {Manifest.permission.CAMERA};
            requestPermissions(permissionRequest, 8675309);
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == RESULT_OK || resultCode != RESULT_CANCELED){
            if(requestCode == CAMERA_REQUEST){
                Bitmap mphoto = (Bitmap) data.getExtras().get("data");

                Intent passPhoto = new Intent(this, Photo.class);
                passPhoto.putExtra("byteArray",mphoto);

                passPhoto.putExtra("Caller", getIntent().getComponent().getClassName());
                startActivity(passPhoto);
            }
        }
    }

Getting the image in other activity like this:

if(getIntent().hasExtra("byteArray")) {
            //ImageView _imv= new ImageView(this);
            /*Bitmap _bitmap = BitmapFactory.decodeByteArray(
                    getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);*/
            Intent intent_camera = getIntent();
            Bitmap camera_img_bitmap = (Bitmap) intent_camera
                    .getParcelableExtra("byteArray");
            //_imv.setImageBitmap(_bitmap);

            View view = mInflater.inflate(R.layout.gallery_item, mGallery, false);
            ImageView img = (ImageView) view
                    .findViewById(R.id.id_index_gallery_item_image);
            //String uri = getPhotos.getString(getPhotos.getColumnIndex(("uri")));
            //Uri mUri = Uri.parse(uri);
            //img.setImageURI(mUri);
            //byte[] blob = getPhotos.getBlob(getPhotos.getColumnIndex("image"));
            //Bitmap bmp = BitmapFactory.decodeByteArray(blob, 0, blob.length);
            //bmpImage.add(bmp);
            img.setImageBitmap(camera_img_bitmap);
            mGallery.addView(view);

        }

My XML:

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

    <ImageView
        android:id="@+id/id_index_gallery_item_image"
        android:layout_width="@dimen/_300sdp"
        android:layout_height="@dimen/_300sdp"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="@dimen/_10sdp"
        android:layout_marginTop="@dimen/_50sdp"
        android:scaleType="centerCrop" />

</RelativeLayout>

What Am I doing wrong?

The method which you have used will not work on all the devices above marshmallow. Follow this,

add this in your manifest.xml

 <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="yourpackagename.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_path"/>
    </provider>

create provider_path in xml folder of your resources.

<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="/storage/emulated/0" path="."/>
</paths>

then add this in your activity, declare a global variable

private Uri mUri;
private static final String CAPTURE_IMAGE_FILE_PROVIDER = "com.yourpackagename.fileprovider";
 private void takePicture() {
    File file = null;
    try {
        file = createImageFile();
        mUri = FileProvider.getUriForFile(context,
                CAPTURE_IMAGE_FILE_PROVIDER, file);

        Log.d("uri", mUri.toString());
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);
        startActivityForResult(cameraIntent, REQUEST_CAMERA_STORAGE);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CAMERA_STORAGE) {
        if (resultCode == RESULT_OK) {
            if (mUri != null) {
                String profileImageFilepath = mUri.getPath().replace("//", "/");
                sendImage(profileImageFilepath);
            }
        } else {
            Toast.makeText(context, "Picture wasn't taken!", Toast.LENGTH_SHORT).show();
        }
    }
}

this is createImageFile()

private File createImageFile() throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "APPNAME-" + timeStamp + ".png";
    File mediaStorageDir = new File(Environment.getExternalStorageDirectory(),
            "FOLDERNAME");
    File storageDir = new File(mediaStorageDir + "/Images");
    if (!storageDir.exists()) {
        storageDir.mkdirs();
    }
    File image = new File(storageDir, imageFileName);
    return image;
}

and finally, pass profileImageFilepath to our next activity to display

In this line I get error:

private static final String CAPTURE_IMAGE_FILE_PROVIDER =      
  "com.yourpackagename.fileprovider";

This line is correct:

private static final String CAPTURE_IMAGE_FILE_PROVIDER = 
  "com.yourpackagename.provider";

instead fileprovider write provider or use this

mUri = FileProvider.getUriForFile(MainActivity.this,
                    BuildConfig.APPLICATION_ID + ".provider", file);

My problem is solved. Thank you very much. Very good work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM