首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >getExternalFilesDir()中的图像

getExternalFilesDir()中的图像
EN

Stack Overflow用户
提问于 2012-05-22 22:41:10
回答 1查看 2.2K关注 0票数 2

我有一些图片存储在getExternalFilesDir()中,我正试图在安卓画廊(cooliris)中展示这些图片。现在我一直在这样做:

代码语言:javascript
运行
复制
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(imgPath,"image/*");
startActivity(intent);

但是什么也没发生。我已将setDataAndType更改为:

代码语言:javascript
运行
复制
intent.setDataAndType(Uri.fromFile(new File(imgPath)),"image/*");

这种方式工作,但它需要5-10秒的画廊,从一个黑屏显示我的图像。

无论如何,解决这个问题或任何更好的方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-22 23:04:57

通过实现文件内容提供程序,您将能够避免这种5-10秒的延迟

代码语言:javascript
运行
复制
import java.io.File;
import java.io.FileNotFoundException;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;

public class FileContentProvider extends ContentProvider {
    private static final String AUTHORITY = "content://com.yourprojectinfo.fileprovider";

    public static Uri constructUri(String url) {
        Uri uri = Uri.parse(url);
        return uri.isAbsolute() ? uri : Uri.parse(AUTHORITY + url);
    }

    public static Uri constructUri(File file) {
        Uri uri = Uri.parse(file.getAbsolutePath());
        return uri.isAbsolute() ? uri : Uri.parse(AUTHORITY
                + file.getAbsolutePath());
    }

    @Override
    public ParcelFileDescriptor openFile(Uri uri, String mode)
            throws FileNotFoundException {
        File file = new File(uri.getPath());
        ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file,
                ParcelFileDescriptor.MODE_READ_ONLY);
        return parcel;
    }

    @Override
    public boolean onCreate() {
        return true;
    }

    @Override
    public int delete(Uri uri, String s, String[] as) {
        throw new UnsupportedOperationException(
                "Not supported by this provider");
    }

    @Override
    public String getType(Uri uri) {
        return "image/jpeg";
    }

    @Override
    public Uri insert(Uri uri, ContentValues contentvalues) {
        throw new UnsupportedOperationException(
                "Not supported by this provider");
    }

    @Override
    public Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) {
        throw new UnsupportedOperationException(
                "Not supported by this provider");
    }

    @Override
    public int update(Uri uri, ContentValues contentvalues, String s,
            String[] as) {
        throw new UnsupportedOperationException(
                "Not supported by this provider");
    }

}

然后你就可以调用

代码语言:javascript
运行
复制
Uri uri = FileContentProvider.constructUri(file);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(uri,"image/*");
startActivity(intent);

这是一个奇怪的变通方法,但我认为这与android如何使用URI打开图像有关。他们的openFile(Uri uri,字符串模式)方法错误/损坏/无法正确解析URI。我不是真的100%确定,但我发现这个变通方法是有效的。

不要忘记在清单中注册提供者

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10704321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档