我有一些图片存储在getExternalFilesDir()
中,我正试图在安卓画廊(cooliris)中展示这些图片。现在我一直在这样做:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(imgPath,"image/*");
startActivity(intent);
但是什么也没发生。我已将setDataAndType更改为:
intent.setDataAndType(Uri.fromFile(new File(imgPath)),"image/*");
这种方式工作,但它需要5-10秒的画廊,从一个黑屏显示我的图像。
无论如何,解决这个问题或任何更好的方法?
发布于 2012-05-22 23:04:57
通过实现文件内容提供程序,您将能够避免这种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);
这是一个奇怪的变通方法,但我认为这与android如何使用URI打开图像有关。他们的openFile(Uri uri,字符串模式)方法错误/损坏/无法正确解析URI。我不是真的100%确定,但我发现这个变通方法是有效的。
不要忘记在清单中注册提供者
https://stackoverflow.com/questions/10704321
复制相似问题