I'm trying to display and image stored in assets folder in external image application.
I've followed this tutorial and tweaked it to use ACTION_VIEW instead of ACTION_SEND in intent action.
My manifest:
<application
..
<provider
android:name="com.voy.sima.utilities.AssetsProvider"
android:authorities="com.voy.sima"
android:grantUriPermissions="true"
android:exported="true" />
...
</application>
My AssestsProvider:
public class AssetsProvider extends ContentProvider {
@Override
public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
AssetManager am = getContext().getAssets();
String file_name = "";
if (uri.getPath().startsWith("/"))
file_name = uri.getPath().substring(1);
else
file_name = uri.getPath();
if (file_name == null)
throw new FileNotFoundException();
AssetFileDescriptor afd = null;
try {
afd = am.openFd(file_name);
} catch (IOException e) {
e.printStackTrace();
}
return afd;
}
@Override
public String getType(Uri p1) {
// TODO: Implement this method
return null;
}
@Override
public int delete(Uri p1, String p2, String[] p3) {
// TODO: Implement this method
return 0;
}
@Override
public Cursor query(Uri p1, String[] p2, String p3, String[] p4, String p5) {
// TODO: Implement this method
return null;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal) {
// TODO: Implement this method
return super.query(uri, projection, selection, selectionArgs, sortOrder, cancellationSignal);
}
@Override
public Uri insert(Uri p1, ContentValues p2) {
// TODO: Implement this method
return null;
}
@Override
public boolean onCreate() {
// TODO: Implement this method
return false;
}
@Override
public int update(Uri p1, ContentValues p2, String p3, String[] p4) {
// TODO: Implement this method
return 0;
}
}
How I generate URIs:
public Uri getWaypointImageUri() {
return Uri.parse("content://com.voy.sima/Fotos/" + this.getImagePath() + ".jpg");
}
My intent:
private void openPictureIntent(waypoint way) {
Intent theIntent = new Intent(Intent.ACTION_VIEW);
Uri uri = way.getWaypointImageUri();
theIntent.setDataAndType(uri, "image/jpeg");
context.startActivity(theIntent);
}
Strange thing is I'm using this same URI generator method inside application with retrofit and is working great. Which leads me to believe the AssetProvider configuration is OK. Moreover, the google+ photo viewer app is displaying images correctly but is the only one. Any other image app fails to display the images (black screen, no errors)
Aucun commentaire:
Enregistrer un commentaire