【发布时间】:2012-03-21 05:47:10
【问题描述】:
您好,我的应用中有一个图库,当您单击缩略图时,它会在图像视图中显示图像。我正在为一个警报对话框设置一个长按监听器,该对话框有 2 个按钮,一个设置为墙纸,一个用于共享。我有共享意图和获取绘图缓存有点工作。它适用于模拟器,但不适用于我的手机。我已经使用了这个网站上的许多示例来实现这一点,但它根本不起作用。它不断强制关闭我手机上的应用程序。任何帮助表示赞赏。
alertDialog.setButton2("Share",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
imgView.setDrawingCacheEnabled(true);
Bitmap b = imgView.getDrawingCache();
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard.getAbsolutePath() + "image.jpg");
try {
file.createNewFile();
OutputStream fos = null;
fos = new FileOutputStream(file);
b.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("Save Example", "Image saved.");
Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri phototUri = Uri.parse("file:///sdcard/image.jpg");
shareIntent.setData(phototUri);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
startActivity(Intent.createChooser(shareIntent, "Share Via"));
}
});
alertDialog.show();
return true;
}
});
更新:这似乎是一个问题
b.compress(CompressFormat.JPEG, 95, fos);
它一直说空指针。
原来空指针实际上是一个只读错误..所以它实际上并没有写入文件我得到一个 ioexception 只读文件系统。为什么要这样做?
【问题讨论】:
-
在尝试写入之前需要查看目录的权限。您必须了解 UNIX 的基本文件系统权限才能进行此分析。
-
您是否在清单文件中使用写权限??
-
是的,我在清单中有 write.external.storage
标签: android bitmap imageview ioexception readonly