我有一个Util类,我在其中使用openFile()方法打开文件。我从另一个类(Browser.class)调用此方法,并尝试向名为ImageViewer.class的第三个类启动特定意图。我的问题是我应该如何在Util类中定义它可以由任何其他类启动的意图?这是我到目前为止的定义。
public static void openFile(final Context context, final File target) {
final String mime = MimeTypes.getMimeType(target);
final Intent i = new Intent(Intent.ACTION_VIEW);
final boolean defaultOpen = true;
if (defaultOpen) {
if(mime.startsWith("image/")){
Intent i1 = new Intent(String.valueOf(ImageViewer.class));
i1.setDataAndType(Uri.fromFile(target), mime);
context.startActivity(i1);
}
}
这是我的logcat:
Process: com.tproductions.Openit, PID: 5796
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=class com.tproductions.Openit.ImageViewer dat=file:///storage/emulated/0/DCIM/100ANDRO/DSC_0001.JPG typ=image/jpeg }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
at android.app.Activity.startActivityForResult(Activity.java:3424)
at android.app.Activity.startActivityForResult(Activity.java:3385)
at android.app.Activity.startActivity(Activity.java:3627)
at android.app.Activity.startActivity(Activity.java:3595)
at com.tproductions.Openit.utils.SimpleUtils.openFile(SimpleUtils.java:318)
at com.tproductions.Openit.Browser.listItemAction(Browser.java:446)
at com.tproductions.Openit.Browser.access$300(Browser.java:46)
at com.tproductions.Openit.Browser$1.onItemClick(Browser.java:208)
答案 0 :(得分:0)
从非Activity
类创建一个intent,使用context
作为构造函数参数
Intent i = new Intent(context, ImageViewer.class);
答案 1 :(得分:0)
由于这是一项非活动课程,我建议您这样做:
private Context mContext;
public utilClassName(Context context) { //pass in the context from the
//Activity class you'll be using the object
mContext = context;
}
然后:
Intent i1 = new Intent(context, ImageViewer.class).
setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i1);