从图库

时间:2016-06-09 15:55:02

标签: android

我创建了一个访问图库,选择图像和裁剪图像的代码,在它之后,代码调用一个保存文件的函数,这个函数给出了一个错误:

  

at com.alexandreccarmo.beerin.user.activities.UserPerfilActivity.onSelectFromGalleryResult(UserPerfilActivity.java:396)                                                                                   在com.alexandreccarmo.beerin.user.activities.UserPerfilActivity.onActivityResult(UserPerfilActivity.java:238)

此错误参考此行:

bm = MediaStore.Images.Media.getBitmap(getApplicationContext()。getContentResolver(),data.getData());

这是功能:

private void onSelectFromGalleryResult(Intent data) throws IOException {

    Bitmap bm=null;
    if (data != null) {
        bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());


        Bitmap thumbnail = bm;
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

        //File destination = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis() + ".jpg");
        File destination = new File(Environment.getExternalStorageDirectory(), "image.jpg");

        FileOutputStream fo;
        try {
            destination.createNewFile();
            fo = new FileOutputStream(destination);
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    ivImage.setImageBitmap(bm);

    dialog = ProgressDialog.show(UserPerfilActivity.this, "", "Alterando a foto...", true);

}

我该如何解决?

这是完整的代码:

private void galleryIntent()
{
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);//

    //crop
    intent.putExtra("crop", "true");
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("outputX", 96);
    intent.putExtra("outputY", 96);
    // retrieve data on return
    intent.putExtra("return-data", true);

    startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);

}

   @Override
     public void onActivityResult(int requestCode, int resultCode,                   Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

        imagePath = this.destinationUpload.getAbsolutePath();
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == SELECT_FILE)
                try {
                    onSelectFromGalleryResult(data);
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
}

完整的堆栈跟踪:

06-09 12:45:43.627 21600-21600/com.alexandreccarmo.beerin E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.alexandreccarmo.beerin/com.alexandreccarmo.beerin.user.activities.UserPerfilActivity}: java.lang.ClassCastException: android.graphics.Bitmap cannot be cast to android.net.Uri
  at android.app.ActivityThread.deliverResults(ActivityThread.java:3205)
  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3248)
  at android.app.ActivityThread.access$1200(ActivityThread.java:140)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:4921)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:511)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
  at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassCastException: android.graphics.Bitmap cannot be cast to android.net.Uri
  at com.alexandreccarmo.beerin.user.activities.UserPerfilActivity.onSelectFromGalleryResult(UserPerfilActivity.java:396)
  at com.alexandreccarmo.beerin.user.activities.UserPerfilActivity.onActivityResult(UserPerfilActivity.java:238)
  at android.app.Activity.dispatchActivityResult(Activity.java:5390)
  at android.app.ActivityThread.deliverResults(ActivityThread.java:3201)
  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3248) 
  at android.app.ActivityThread.access$1200(ActivityThread.java:140) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285) 
  at android.os.Handler.dispatchMessage(Handler.java:99) 
  at android.os.Looper.loop(Looper.java:137) 
  at android.app.ActivityThread.main(ActivityThread.java:4921) 
  at java.lang.reflect.Method.invokeNative(Native Method) 
  at java.lang.reflect.Method.invoke(Method.java:511) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 
  at dalvik.system.NativeStart.main(Native Method) 
06-09 12:50:43.662 21600-21600/com.alexandreccarmo.beerin I/Process: Sending signal. PID: 21600 SIG: 9

1 个答案:

答案 0 :(得分:0)

查看堆栈跟踪...

  

引起:java.lang.ClassCastException:android.graphics.Bitmap无法强制转换为android.net.Uri

基于Android的文档:

  

getBitmap在API级别1中添加   Bitmap getBitmap (ContentResolver cr, Uri url)

看起来你传递给第二个参数的实际上是一个android.graphics.Bitmap对象而不是android.net.Uri

这表示您的Intent数据实际上是Bitmap,因此data.getData()正在返回该方法不喜欢的Bitmap

您应该可以直接指定bm = data.getData(),因为图库选择Intent正在返回。