Android Gallery 3D不会返回Honeycomb中的位图

时间:2011-06-26 02:46:29

标签: android bitmap android-intent

我正在使用这两个类来请求和检索图库中的图片。这段代码适用于Gingerbread及以下版本,但在我的Xoom上的Honeycomb中它失败了。

我看到它的行为它写了一个空白文件但画廊没有将所选图片写入该文件。此外,该文件在Windows中不可见我必须转到DDMS选项卡才能看到要创建的文件。它拥有所有者和团体的权限,但不是每个人。

改编自: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 = Environment.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;
}

1 个答案:

答案 0 :(得分:0)

启动获取照片的意图。

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

返回时接受照片。

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

这个问题太疯狂了,我写了一篇关于如何正确地做到这一点的文章。或者至少是最好的方式。

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