我正在尝试使用活动中的按钮从res / raw文件夹中打开jpg文件。但是,在启动和测试应用程序时,我收到错误消息:
11-09 10:20:51.654: E/AndroidRuntime(3247): android.content.ActivityNotFoundException:
No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///android.resource:/com.image.imagetest/2131034112 typ=application/jpg flg=0x4000000 }`
这是我的代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_image, container, false);
Button button = (Button) rootView.findViewById(R.id.Image1);
button.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
File jpgFile = new File("android.resource://com.image.imagetest/" + R.raw.mainimage);
Uri path = Uri.fromFile(jpgFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, "application/jpg");
startActivity(intent);
}
});
return rootView;
}
我在这里遗漏了什么:-)?
修改
我现在已经完成了这个,但是现在当启动应用程序测试时,我会收到一条消息,说“点击按钮无法找到项目”。这是我的代码:
public void onClick(final View v) {
File jpgFile = new File("android.resource://com.image.imagetest/" + R.raw.mainimage);
Uri path = Uri.fromFile(jpgFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, "image/jpeg");
startActivity(intent);
}
答案 0 :(得分:0)
我认为您在创建file:/
时忘记添加intent
:
File jpgFile = new File("file:/android.resource://com.image.imagetest/" + R.raw.mainimage);
Uri path = Uri.fromFile(jpgFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
看看这里:No Activity found to handle Intent : android.intent.action.VIEW
答案 1 :(得分:0)
没有理由在这里使用File
。把Uri放在意图中:
public void onClick(final View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("android.resource://com.image.imagetest/" + R.raw.mainimage));
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
您的解决方案有什么问题?
image/jpeg
代替application/jpg