java - 如何在android中选择和裁剪图像?

标签 java android image android-intent crop

嘿,我目前正在制作动态壁纸,我允许用户选择一个图像来支持我的效果。

目前我有:

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 为空。如果我注释掉

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

Logcat 没有给我空指针异常,我可以对图像做我想做的事。那么,这里有什么问题呢?有谁知道我该如何解决这个问题?谢谢你的时间。

最佳答案

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

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

关于java - 如何在android中选择和裁剪图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2085003/

相关文章:

java - 为 spring-boot redis 缓存配置配置一个新的序列化器

java - 在 axis2-wsdl2code-maven-plugin 中设置 -Eosv 属性

java - 如何在表格中输出素数

android - Eclipse 自动创建 fragment 布局,如何禁用它

javascript - 选择,然后使用 jQuery 调整具有一定比例的图像

java - 编译和执行java文件批处理文件问题

Android Runtime.getRuntime().exec() 通过目录导航

Java 代码在 Android Studio 中未命中 IF 或 ELSE

c# - 在 WP7 上显示动画图像

iphone - 如何定位正确的图片路径