代码之家  ›  专栏  ›  技术社区  ›  user3971448

我在android中使用getParcelable检索位图文件时出错

  •  0
  • user3971448  · 技术社区  · 9 年前

    我正在尝试获取我传递给intent的位图文件,但getParcelable()方法每次都返回null。

    以下是我发送位图图像的java代码:

    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    fileUri = ImageSharing.getOutputMediaFileUri(StaticMembers.MEDIA_TYPE_IMAGE, false);
    
    List<Intent> cameraIntents = new ArrayList<Intent>();
    PackageManager packageManager = getActivity().getPackageManager();
    List<ResolveInfo> listCam = packageManager.queryIntentActivities(cameraIntent, 0);
    for (ResolveInfo res : listCam) {
        String packageName = res.activityInfo.packageName;
        Intent intent = new Intent(cameraIntent);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        intent.setPackage(packageName);
        intent.putExtra("return-data", true);
        cameraIntents.add(intent);
    }
    
    Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    galleryIntent.setType(StaticMembers.IMAGE_TYPE);
    
    Intent chooserIntent;
    if (null == newmobilelangs && null == newmobilelangs.getCommon().getCompleteAction()) {
        chooserIntent = Intent.createChooser(galleryIntent, "Complete action using");
    } else {
        chooserIntent = Intent.createChooser(galleryIntent, newmobilelangs.getCommon()
                .getCompleteAction());
    }
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
    startActivityForResult(chooserIntent, 1);
    

    下面是我试图检索图像的java代码:

    Bitmap profilePic;
    String fileName, filePath;
    Log.i("what is data?", data.toString());
    Bundle extras = data.getExtras();
    profilePic = extras.getParcelable("data");
    

    上面代码的最后一行出现问题:

    profilePic = extras.getParcelable("data");
    

    以下是来自SettingFragment.java的onActivityResult()方法

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            if (resultCode == Activity.RESULT_OK) {
                if (requestCode == 1) {
                    boolean isCamera;
                    isCamera = ((data == null) || (data.hasExtra(MediaStore.EXTRA_OUTPUT)));
    
                    Intent intent = new Intent("com.android.camera.action.CROP");
                    if (isCamera) {
                        intent.setDataAndType(fileUri, StaticMembers.IMAGE_TYPE);
                    } else {
                        intent.setDataAndType(data.getData(), StaticMembers.IMAGE_TYPE);
                    }
    
                    intent.putExtra("crop", "true");
                    intent.putExtra("aspectX", 1);
                    intent.putExtra("aspectY", 1);
                    // intent.putExtra("outputX", 150);
                    // intent.putExtra("outputY", 150);
                    intent.putExtra("scale", true);
                    intent.putExtra("noFaceDetection", true);
                    intent.putExtra("return-data", true);
                    tempFileURI = ImageSharing.getOutputMediaFileUri(StaticMembers.MEDIA_TYPE_IMAGE, false);
                    Log.i("Yes!", tempFileURI.toString());
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, tempFileURI);
                    startActivityForResult(intent, 2);
                } else if (requestCode == 2) {
                    Bitmap profilePic;
                    String fileName, filePath;
                    Log.i("what is data?", data.toString());
                    Bundle extras = data.getExtras();
                    profilePic = extras.getParcelable("data");
    

    以下是日志:

    09-14 11:12:35.271  24647-24647/com.flowonme.chat I/what is data?﹕ Intent { dat=file:///storage/sdcard0/CometChat Images/IMG20150914111228.jpg typ=image/jpeg }
    09-14 11:12:35.274  24647-24647/com.flowonme.chat W/System.err﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' on a null object reference
    09-14 11:12:35.275  24647-24647/com.flowonme.chat W/System.err﹕ at com.inscripts.fragments.SettingsFragment.onActivityResult(SettingsFragment.java:318)
    09-14 11:12:35.275  24647-24647/com.flowonme.chat W/System.err﹕ at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:163)
    09-14 11:12:35.275  24647-24647/com.flowonme.chat W/System.err﹕ at com.inscripts.utils.SuperActivity.onActivityResult(SuperActivity.java:142)
    09-14 11:12:35.275  24647-24647/com.flowonme.chat W/System.err﹕ at android.app.Activity.dispatchActivityResult(Activity.java:6192)
    09-14 11:12:35.275  24647-24647/com.flowonme.chat W/System.err﹕ at android.app.ActivityThread.deliverResults(ActivityThread.java:3570)
    09-14 11:12:35.275  24647-24647/com.flowonme.chat W/System.err﹕ at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
    09-14 11:12:35.275  24647-24647/com.flowonme.chat W/System.err﹕ at android.app.ActivityThread.access$1300(ActivityThread.java:151)
    09-14 11:12:35.275  24647-24647/com.flowonme.chat W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
    0
    
    2 回复  |  直到 9 年前
        1
  •  1
  •   ρяσѕρєя K    9 年前

    如下所示:

    意图{dat=file:///storage/sdcard0/CometChat Images/IMG20150914111228.jpg typ=image/jpeg}

    正在获取响应中图像的URI。因此从URI获取位图为:

    Uri imageUri = data.getData();
    profilePic = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
    
        2
  •  0
  •   Egos Zhang    9 年前

    我遇到过一些关于Android 5.0手机的问题。我认为这是关于 URI . 你可以试试这个。

    Bitmap avatar = null;
    if (data.hasExtra("data"))
    {
        avatar = data.getParcelableExtra("data");
    }
    else
    {
        BitmapFactory.Options options = new BitmapFactory.Options();
        InputStream is = null;
        try
        {
                is = getContentResolver().openInputStream(data.getData());
                avatar = BitmapFactory.decodeStream(is, null, options);
        }
        catch (Exception e)
        {
            LogUtils.error("file not found");
        }
        finally
        {
            try
            {
                is.close();
            }
            catch (Exception e)
            {
            }
        }
    }
    

    我建议你使用 乌里 . Bitmap 也许OOM。