不同 class 的意图定义
Intent definition for different class
我有一个 Util class,其中有用于打开文件的 openFile() 方法。我从另一个 class (Browser.class) 调用此方法,并尝试向名为 ImageViewer.class 的第三个 class 启动特定意图。我的问题是,我应该如何在 Util class 中定义可以由任何其他 class 启动的意图?这是我到目前为止的定义。
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.access0(Browser.java:46)
at com.tproductions.Openit.Browser.onItemClick(Browser.java:208)
从非 Activity
class 创建一个意图,使用 context
作为构造函数参数
Intent i = new Intent(context, ImageViewer.class);
因为这是非activity class,我建议你这样做:
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);
我有一个 Util class,其中有用于打开文件的 openFile() 方法。我从另一个 class (Browser.class) 调用此方法,并尝试向名为 ImageViewer.class 的第三个 class 启动特定意图。我的问题是,我应该如何在 Util class 中定义可以由任何其他 class 启动的意图?这是我到目前为止的定义。
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.access0(Browser.java:46)
at com.tproductions.Openit.Browser.onItemClick(Browser.java:208)
从非 Activity
class 创建一个意图,使用 context
作为构造函数参数
Intent i = new Intent(context, ImageViewer.class);
因为这是非activity class,我建议你这样做:
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);