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

查看:24
本文介绍了如何在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();
        } 
    }

当我的代码运行时,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.

推荐答案

我也遇到过这个问题.你可以试试这个代码.它对我来说很好

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天全站免登陆