Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

android - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...