画像URIを抽出しました。次に、Androidのデフォルトの画像ビューアで画像を開きます。またはさらに良いことに、ユーザーは画像を開くために使用するプログラムを選択できます。ファイルを開こうとすると、ファイルエクスプローラーのようなものが提供されます。
14 に答える
受け入れられた答えは私のために働いていませんでした、
何がうまくいったか:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + "/sdcard/test.jpg"), "image/*");
startActivity(intent);
アプリがAndroidN(7.0)以降をターゲットにしている場合は、上記の回答( "Uri.fromFile"メソッドの)を使用しないでください。機能しないためです。
代わりに、ContentProviderを使用する必要があります。
たとえば、画像ファイルが外部フォルダにある場合は、これを使用できます(ここで作成したコードと同様):
File file = ...;
final Intent intent = new Intent(Intent.ACTION_VIEW)//
.setDataAndType(VERSION.SDK_INT >= VERSION_CODES.N ?
FileProvider.getUriForFile(this,getPackageName() + ".provider", file) : Uri.fromFile(file),
"image/*").addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
マニフェスト:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
res / xml /provider_paths.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<!--<external-path name="external_files" path="."/>-->
<external-path
name="files_root"
path="Android/data/${applicationId}"/>
<external-path
name="external_storage_root"
path="."/>
</paths>
画像がアプリのプライベートパスにある場合は、リンクに「OpenFileProvider」を作成したので、独自のContentProviderを作成する必要があります。
自問して、自分自身にも答えてください:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/external/images/media/16"))); /** replace with your own uri */
また、ファイルを表示するために使用するプログラムも尋ねられます。
それを使ってみてください:
Uri uri = Uri.fromFile(entry);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
String mime = "*/*";
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
if (mimeTypeMap.hasExtension(
mimeTypeMap.getFileExtensionFromUrl(uri.toString())))
mime = mimeTypeMap.getMimeTypeFromExtension(
mimeTypeMap.getFileExtensionFromUrl(uri.toString()));
intent.setDataAndType(uri,mime);
startActivity(intent);
Vikasの回答に基づいていますが、わずかな変更が加えられています。Uriはパラメーターによって受信されます。
private void showPhoto(Uri photoUri){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(photoUri, "image/*");
startActivity(intent);
}
これは、AndroidN以下で作業している場合に役立つ可能性があります
File file=new File(Environment.getExternalStorageDirectory()+"/directoryname/"+filename);
Uri path= FileProvider.getUriForFile(MainActivity.this,BuildConfig.APPLICATION_ID + ".provider",file);
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path,"image/*");
intent.setFlags(FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION); //must for reading data from directory
この問題に対するはるかにクリーンで安全な答え(文字列をハードコーディングするべきではありません):
public void openInGallery(String imageId) {
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI.buildUpon().appendPath(imageId).build();
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
EXTERNAL_CONTENT_URIのパスの最後に画像IDを追加するだけです。次に、表示アクションとURIを使用してインテントを起動します。
画像IDは、コンテンツリゾルバーのクエリから取得されます。
上記のすべての回答が画像を開かない..2回目に開こうとすると、画像ではなくギャラリーが表示されます。
さまざまなSOの回答を組み合わせて解決策を得ました。
Intent galleryIntent = new Intent(Intent.ACTION_VIEW, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setDataAndType(Uri.fromFile(mImsgeFileName), "image/*");
galleryIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(galleryIntent);
これは私のためだけに働いた。
を使用してファイルを表示する場合の問題は、パスの解析Intent.ACTION_VIEW
を渡す場合です。Uri
すべての場合に機能するわけではありません。この問題を解決するには、次を使用する必要があります。
Uri.fromFile(new File(filePath));
それ以外の:
Uri.parse(filePath);
編集
これが私の完全なコードです:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(mediaFile.filePath)), mediaFile.getExtension());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
情報
MediaFile
データベースのファイルをオブジェクトにラップするための私のドメインクラスです。
ファイル拡張子にwithをMediaFile.getExtension()
返します。例:String
Mimetype
"image/png"
追加コード:ファイル(拡張子)を表示するために必要
import android.webkit.MimeTypeMap;
public String getExtension () {
MimeTypeMap myMime = MimeTypeMap.getSingleton();
return myMime.getMimeTypeFromExtension(MediaFile.fileExtension(filePath));
}
public static String fileExtension(String path) {
if (path.indexOf("?") > -1) {
path = path.substring(0, path.indexOf("?"));
}
if (path.lastIndexOf(".") == -1) {
return null;
} else {
String ext = path.substring(path.lastIndexOf(".") + 1);
if (ext.indexOf("%") > -1) {
ext = ext.substring(0, ext.indexOf("%"));
}
if (ext.indexOf("/") > -1) {
ext = ext.substring(0, ext.indexOf("/"));
}
return ext.toLowerCase();
}
}
さらにコードが必要な場合はお知らせください。
私はこれを使用しますそれは私のために働きます
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), 1);
ファイルプロバイダーを使用した私のソリューション
private void viewGallery(File file) {
Uri mImageCaptureUri = FileProvider.getUriForFile(
mContext,
mContext.getApplicationContext()
.getPackageName() + ".provider", file);
Intent view = new Intent();
view.setAction(Intent.ACTION_VIEW);
view.setData(mImageCaptureUri);
List < ResolveInfo > resInfoList =
mContext.getPackageManager()
.queryIntentActivities(view, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo: resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
mContext.grantUriPermission(packageName, mImageCaptureUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
view.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(mImageCaptureUri, "image/*");
mContext.startActivity(intent);
}
写真やギャラリーアプリケーションを使用する機会はほとんどありませんが(存在する可能性があります)、コンテンツビューアを試すことができます。
こちらで同様の質問に対する別の回答を確認してください
私の解決策
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory().getPath()+"/your_app_folder/"+"your_picture_saved_name"+".png")), "image/*");
context.startActivity(intent);
uriはfileuriではなくcontenturiである必要があります。FileProviderによってcontentUriを次のように取得できます。
Uri contentUri = FileProvider.getUriForFile(getContext(),"com.github.myApp",curFile);
マニフェストファイルにプロバイダーを追加することを忘れないでください。
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.github.myApp"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>