【问题标题】:Android Gallery 3D doesn't return bitmaps in HoneycombAndroid Gallery 3D 不会在 Honeycomb 中返回位图
【发布时间】:2012-04-29 18:26:38
【问题描述】:

我正在使用这 2 个类从图库中请求和检索图片。此代码在 Gingerbread 及以下版本中运行良好,但在我的 Xoom 上的 Honeycomb 中却失败了。

我看到它的行为会写入一个空白文件,但画廊不会将所选图片写入该文件。此外,该文件在 Windows 中不可见,我必须转到 DDMS 选项卡才能看到创建的文件。它对所有者和组具有 rw 访问权限,但不是所有人。

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

【问题讨论】:

    标签: android bitmap android-intent


    【解决方案1】:

    启动意图以获取照片。

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-13
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      • 1970-01-01
      相关资源
      最近更新 更多