Android-android (miui系统)资源图片裁剪界面无法打开?

小组事务管理 小组事务管理 主题:974 回复:1955

Android-android (miui系统)资源图片裁剪界面无法打开?

浮生未歇 发布于 2017-08-04 字数 1016 浏览 1029 回复 2

想写一个拍照做用户头像的功能,现在遇到的问题就是,照片拍完了,我也能找到,但是还想做一个剪切的功能,就希望调用系统的剪切就行了, 于是出现了以下的代码。 这代码在正常的系统下都是可以达到要求的,可是小小米手机 和 小米的rom 下面,就死活调不出图片剪切界面。 特来求救,谁能告诉我小米下面应该若何编写? 万分感谢!

protected void doCropPhoto(Bitmap data) {
Intent intent = getCropImageIntent(data);
startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
}

public static Intent getCropImageIntent(Bitmap data) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
intent.putExtra("data", data);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 128);
intent.putExtra("outputY", 128);
intent.putExtra("return-data", true);
return intent;
}

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

支持 Markdown 语法,需要帮助?

评论(2

想挽留 2017-10-12 2 楼

哥们,我用你的例子在小米1上拍照时裁剪还是掉不出裁剪框,你那有这个问题吗

虐人心 2017-08-12 1 楼

修正了下,最终代码如下, 大米小米应该都木有问题了

package com.mumayi.test;

import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.InputStream;

import android.content.ContentResolver; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView;

import com.mumayi.miaochuan.R;

public class SdCardUriTestActivity extends MCBaseActivity { /* Called when the activity is first created. / private Button btnTake = null; private ImageView lblImage = null; private Button btnTake_2 = null; private static final int PHOTO_PICKED_WITH_DATA = 3021; private static final int CAMERA_WITH_DATA = 3023;

private byte[] mContent; 
private ContentResolver resolver = null;
private Bitmap photo= null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mc_text_main);

    btnTake = (Button) findViewById(R.id.btnTake);
    btnTake_2 = (Button) findViewById(R.id.btnTake_2);
    lblImage = (ImageView) findViewById(R.id.lblImage);
    btnTake.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, CAMERA_WITH_DATA);
            resolver = getContentResolver(); 
        }
    });

    btnTake_2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent intent = new Intent(Intent.ACTION_GET_CONTENT); // "android.intent.action.GET_CONTENT"
            intent.setType("image/*");
            intent.putExtra("crop", "true");
            intent.putExtra("aspectX", 1);
            intent.putExtra("aspectY", 1);
            intent.putExtra("outputX", 128);
            intent.putExtra("outputY", 128);
            intent.putExtra("return-data", true);
            startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);

        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    if (resultCode != RESULT_OK)
        return;

    try {
        switch (requestCode) {
        case CAMERA_WITH_DATA:
            photo = data.getParcelableExtra("data");
            if (photo != null) {
                doCropPhoto(photo);
            }else{
                doCropPhoto(data.getData());
            }
        case PHOTO_PICKED_WITH_DATA:
            photo = data.getParcelableExtra("data");
                if(photo == null){
                      //获得图片的uri
                    Uri originalUri = data.getData();
                    //将图片内容解析成字节数组
                    mContent=readStream(resolver.openInputStream(Uri.parse(originalUri.toString())));
                    //将字节数组转换为ImageView可调用的Bitmap对象
                    photo = getPicFromBytes(mContent, null); 
                }
            if (photo != null) {
                lblImage.setImageBitmap(photo);
            }

        }
    } catch (Exception e) {
        L(e);
    }
}

protected void doCropPhoto(Bitmap data) {
    Intent intent = getCropImageIntent(data);
    startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
}
protected void doCropPhoto(Uri uri) {
    Intent intent = getCropImageIntent(uri);
    startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
}

public static Intent getCropImageIntent(Bitmap data) {
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setType("image/*");
    intent.putExtra("data", data);
    intent.putExtra("crop", "true");
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("outputX", 128);
    intent.putExtra("outputY", 128);
    intent.putExtra("return-data", true);
    return intent;
}

public static Intent getCropImageIntent(Uri uri) {
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(uri, "image/*");
    intent.putExtra("crop", "true");
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("outputX", 128);
    intent.putExtra("outputY", 128);
    intent.putExtra("return-data", true);
    return intent;
}

public static Bitmap getPicFromBytes(byte[] bytes, BitmapFactory.Options opts) {
    if (bytes != null)
        if (opts != null)
            return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,opts);
        else
            return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    return null;
} 

public static byte[] readStream(InputStream inStream) throws Exception {
    byte[] buffer = new byte[1024];
    int len = -1;
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    while ((len = inStream.read(buffer)) != -1) {
             outStream.write(buffer, 0, len);
    }
    byte[] data = outStream.toByteArray();
    outStream.close();
    inStream.close();
    return data;

}

@Override
protected void onDestroy() {
    if(photo!=null)
        photo.recycle();
    super.onDestroy();
} 

}