将图像传递给另一个活动时,图像会丢失其原始结果 [英] Image loses it's original result when passing it to another activity

查看:25
本文介绍了将图像传递给另一个活动时,图像会丢失其原始结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的应用程序中开发相机功能.我正在捕获图像并将其传递给另一个活动.我面临的问题是,当我在另一个活动中显示图像时,由于某种原因,它会丢失其原始结果(被像素化).这就是我的做法:

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

        }

我的 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>

我做错了什么?

推荐答案

您使用的方法不适用于棉花糖以上的所有设备.按照这个,

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

将此添加到您的 manifest.xml

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>

在资源的 xml 文件夹中创建 provider_path.

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

这是 createImageFile()

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

最后,将 profileImageFilepath 传递给我们下一个要显示的活动

and finally, pass profileImageFilepath to our next activity to display

这篇关于将图像传递给另一个活动时,图像会丢失其原始结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆