开发者

Android Gallery 3D doesn't return bitmaps in Honeycomb

I'm using these 2 classes to request and retrieve the picture from the Gallery. This code works well in Gingerbread and below but in Honeycomb on my Xoom it fails.

The behavior I see it it writes a blank file but the gallery doesn't write the chosen picture to that file. In addition, the file is not visible in Windows I have to go to the DDMS tab to see the file be created. It has rw access for owner and group but not everybody.

Adapted From: Retrieve Picasa Image for Upload from Gallery

public static class GetImage implements IIntentBuilderForResult {
    public String TypeFilter = "image/*";
    public boolean ForceDefaultHandlers = false;
    public Bitmap.CompressFormat CompressFormat = Bitmap.CompressFormat.PNG;

    private Intent intent = null;
    private String TemporaryImagePath = null;

    public void prepareIntent(Context context) {
        try {
            File dir = En开发者_C百科vironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            dir.mkdirs();
            File tempFile = new File(dir, "galleryresult-temp.png");

            //.createTempFile("GalleryResult", ".png");
            TemporaryImagePath = tempFile.getAbsolutePath();

            tempFile.getParentFile().mkdirs();

            tempFile.createNewFile();
            Logger.d("IsFile= " + tempFile.isFile());
            tempFile.setWritable(true, false);
            tempFile.setReadable(true, false);

            intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType(TypeFilter);

            if (ForceDefaultHandlers) {
                intent.addCategory(Intent.CATEGORY_DEFAULT);
            }

            final Uri uri = Uri.fromFile(tempFile);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            final String formatName = CompressFormat.name();
            intent.putExtra("outputFormat", formatName);
        } catch (Exception e) {

        }
    }

    public Intent getIntent() {
        return intent;
    }

    public Bundle getResultBundle() {
        Bundle data = new Bundle();
        data.putString("transientImagePath", TemporaryImagePath);
        return data;
    }
}

public abstract static class GetImageResult extends ActivityResultHandlerHelper {

    public Bitmap bitmapResult = null;

    public void onPrepareResult() {
        bitmapResult = null;
        Uri imageUri = null;
        String filePath = null;
        boolean fromTransientPath = false;
        String tempFilePath = null;

        if(resultBundle != null) {
            tempFilePath = resultBundle.getString("transientImagePath");

            File tempFile = new File(tempFilePath);
            imageUri = Uri.fromFile(tempFile);
        }
        if(imageUri == null || imageUri.toString().length() == 0) {
            imageUri = data.getData();
        } else {
            fromTransientPath = true;
        }

        if(imageUri != null) {

            if(imageUri.getScheme().equals("file")) {
                filePath = imageUri.getPath();
            } else if(imageUri.getScheme().equals("content")) {
                filePath = findPictureFilePath(context, imageUri);
            }

            if(filePath != null) {
                bitmapResult = BitmapFactory.decodeFile(filePath);
                if(fromTransientPath) {
                    //File delTarget = new File(filePath);
                    //delTarget.delete();
                }
            }
        }
    }
}

public static final String findPictureFilePath(Context context, Uri dataUri) {
    String filePath = null;
    final String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = null;
    try {
        cursor = context.getContentResolver().query(dataUri, projection,
                null, null, null);
        int data_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        if (cursor.moveToFirst()) {
            filePath = cursor.getString(data_index);
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return filePath;
}


Launch intent to get the photo.

final Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
activity.startActivityForResult(intent, requestCode);

Accept the photo on return.

final InputStream is = context.getContentResolver().openInputStream(intent.getData());
final Bitmap imageData = BitmapFactory.decodeStream(is, null, options);
is.close();

This issue was so maddening that I wrote a whole article about how to do it properly. Or at least the best possible way.

http://androidfragments.blogspot.com/2012/02/loading-bitmaps-from-gallery.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜