嗨,我在我的应用程序中有一个画廊,当你点击缩略图时,它会在图像视图中显示图像。我正在设置一个警报对话框的Long Click监听器,它有两个按钮,一个设置为墙纸,另一个设置为共享。我有共享意图和get绘图缓存的一些工作。它可以在模拟器中工作,但不能在我的手机上工作。我已经在这个网站上使用了很多例子来实现这一点,但它根本不起作用。它会强制关闭我手机上的应用程序。任何帮助都是非常感谢的。
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);它一直在说空指针。
事实证明,空指针实际上是一个异常error..So,它并不是真的在写文件,我得到了一个只读文件系统。它为什么要这么做?
发布于 2012-03-23 13:46:12
好了,这就是我最终要做的,让它分享照片,以防其他人需要它。原来我的ImageView没有呈现为BitMap,所以它是make files,里面什么都没有。因此,我只是从它的位置调用drawable并保存它。对于这个简单的图库,它的代码要干净得多。我在一个警告对话框中有它,但它也可以设置为常规按钮。
alertDialog.setButton2("Share",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
long currentTime = System.currentTimeMillis();
imgView.setDrawingCacheEnabled(true);
String newFolder = "/CFW";
String extStorageDirectory = Environment
.getExternalStorageDirectory()
.toString();
File sdCard = new File(extStorageDirectory
+ newFolder);
sdCard.mkdir();
File file = new File(sdCard.getAbsolutePath()
+ "/cfw" + currentTime + ".jpg");
try {
InputStream is = getResources()
.openRawResource(pics[position]);
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.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 photoUri = Uri
.parse("file:///sdcard/CFW/cfw"
+ currentTime + ".jpg");
shareIntent.setData(photoUri);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_STREAM,
photoUri);
startActivity(Intent.createChooser(shareIntent,
"Share Via"));
}
});
alertDialog.show();
return true;
}
});
}https://stackoverflow.com/questions/9799226
复制相似问题