如何选择和裁剪图像中的android? [英] How to select and crop an image in android?

查看:112
本文介绍了如何选择和裁剪图像中的android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我目前工作的一个动态壁纸,我允许用户选择一个图像,该图像会背着我的影响。

Hey, I am currently working on a live wallpaper and I allow the user to select an image which will go behind my effects.

目前我有:

Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            i.putExtra("crop", "true");
            startActivityForResult(i, 1);

和略低于说:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (requestCode == 1)
        if (resultCode == Activity.RESULT_OK) {
          Uri selectedImage = data.getData();
          Log.d("IMAGE SEL", "" + selectedImage);
          // TODO Do something with the select image URI
          SharedPreferences customSharedPreference = getSharedPreferences("imagePref", Activity.MODE_PRIVATE);
          SharedPreferences.Editor editor = customSharedPreference.edit();
          Log.d("HO", "" + selectedImage);
          editor.putString("imagePref", getRealPathFromURI(selectedImage));
          Log.d("IMAGE SEL", getRealPathFromURI(selectedImage));
          editor.commit();
        } 
    }

在我的code为跑,logcat的告诉我,selectedImage为空。如果我注释掉

When my code is ran, Logcat tells me that selectedImage is null. If I comment out the

i.putExtra("crop", "true"):

LogCat中并没有给我的空指针异常,我能够做我想做的形象。那么,有什么问题吗?任何人不会有任何想法如何,我可以解决这一问题?谢谢您的时间。

Logcat does not give me the null pointer exception, and I am able to do what I want with the image. So, what is the problem here? Does any one have any idea how I can fix this? Thanks, for your time.

推荐答案

我也面临这个问题。你可以用这个code尝试。它的工作对我罚款

I have also faced this problem .You can try with this code. Its working fine for me

private static final String TEMP_PHOTO_FILE = "temporary_holder.jpg";  

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
photoPickerIntent.putExtra("crop", "true");
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(photoPickerIntent, REQ_CODE_PICK_IMAGE);

private Uri getTempUri() {
    return Uri.fromFile(getTempFile());
}

private File getTempFile() {

    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

        File file = new File(Environment.getExternalStorageDirectory(),TEMP_PHOTO_FILE);
        try {
            file.createNewFile();
        } catch (IOException e) {}

        return file;
    } else {

        return null;
    }
}

protected void onActivityResult(int requestCode, int resultCode,
        Intent imageReturnedIntent) {

    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    switch (requestCode) {
        case REQ_CODE_PICK_IMAGE:
            if (resultCode == RESULT_OK) {  
                if (imageReturnedIntent!=null) {

                    File tempFile = getTempFile();

                    String filePath= Environment.getExternalStorageDirectory()
                        +"/"+TEMP_PHOTO_FILE;
                    System.out.println("path "+filePath);


                    Bitmap selectedImage =  BitmapFactory.decodeFile(filePath);
                    _image = (ImageView) findViewById(R.id.image);
                    _image.setImageBitmap(selectedImage );

                    if (tempFile.exists()) tempFile.delete();
                }
            }
    }       
}

这篇关于如何选择和裁剪图像中的android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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