通過實施文件內容提供商,你將能夠避免這一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%確定,但我發現此解決方法是有效的。
不要忘記註冊商在清單
它沒有工作...我收到以下錯誤: 'java.lang.UnsupportedOperationException:此provider'不支持 05-22 18 :53:26.959:E/DatabaseUtils(26789):\t在com.mypackage.provider.FileContentProvider.query(FileContentProvider.java:58) –
那麼,有些東西是查詢該URI是錯誤的。什麼都不應該查詢那個URI。它是否說什麼叫它查詢? – dymmeh
在乞討的錯誤說: '05-22 19:04:57.259:E/DatabaseUtils(26789):寫入例外包裹' 「它說什麼是調用查詢嗎?我怎麼能看到? –