通过实现文件内容提供程序,您将能够避免这 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, String mode) 方法是错误的/损坏的/无法正确解析 URI。虽然我不是 100% 确定,但我发现这种解决方法是有效的。
不要忘记在清单中注册提供者