English 中文(简体)
Get External FilesDir () 中的图像
原标题:Image in getExternalFilesDir()

I have some images stored in getExternalFilesDir() and i am trying to show those images in the android gallery (cooliris). Right now i have been doing this:

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(imgPath,"image/*");
startActivity(intent);

But nothing happens. I have changed the setDataAndType to this:

intent.setDataAndType(Uri.fromFile(new File(imgPath)),"image/*");

但画廊需要5到10秒 从黑屏幕上展示我的形象

不管怎么说,解决这个还是更好的方法?

最佳回答

通过实施文件内容提供者, 您可以避免5- 10 第二次延迟 。

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

}

然后你可以打电话

Uri uri = FileContentProvider.constructUri(file);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(uri,"image/*");
startActivity(intent);

这是一个奇怪的变通方法, 但我认为它和机器人如何使用 URI 打开图像有关。 它们的开放式File( Uri uri, string 模式) 方法是错误的 / 折断 / 无法正确解决 URI 。 虽然我并不完全确定, 但我发现这种变通方法是有效的 。

请勿忘记在货单上向提供商登记。

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签