我需要在android中发送彩信所以给出了如下代码,来自drawable的图像首先保存在SD卡中,然后取回到短信机构,但点击“发送”按钮后,我得到的图像如下所示。从SD卡下载的已保存图像无法打开,我收到错误为“无效文件”。使用图像名称检查所有内容都是小写字母。 我哪里出错了,请告诉我。
意图代码
private void doSendIntent(String subject, String text) {
try {
Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
// sendIntent.setType("message/rfc822");
sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, text);
sendIntent.setType("image/png");
Bitmap bbicon;
bbicon = BitmapFactory.decodeResource(getResources(),
R.drawable.gicon);
String extStorageDirectory = Environment
.getExternalStorageDirectory().toString();
OutputStream outStream = null;
File f = new File(extStorageDirectory + "/Download/",
"gicon.png");
try {
outStream = new FileOutputStream(f);
bbicon.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (Exception e) {
}
File fl = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/Download/", "gicon.png");
Uri uri = Uri.fromFile(fl);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
this.cordova).getContext().getPackageName(), 0);
this.cordova.startActivityForResult(this, sendIntent, 0);
}
private Resources getResources() {
// TODO Auto-generated method stub
return null;
}
}
Logcat错误
09-20 11:58:35.200: E/Mms/media(323): IOException caught while opening or reading stream
09-20 11:58:35.200: E/Mms/media(323): java.io.FileNotFoundException: /mnt/sdcard/Download/gicon.png (Permission denied)
09-20 11:58:35.200: E/Mms/media(323): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
09-20 11:58:35.200: E/Mms/media(323): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
09-20 11:58:35.200: E/Mms/media(323): at java.io.FileInputStream.<init>(FileInputStream.java:80)
09-20 11:58:35.200: E/Mms/media(323): at java.io.FileInputStream.<init>(FileInputStream.java:132)
09-20 11:58:35.200: E/Mms/media(323): at android.content.ContentResolver.openInputStream(ContentResolver.java:317)
09-20 11:58:35.200: E/Mms/media(323): at com.android.mms.model.MediaModel.initMediaSize(MediaModel.java:280)
09-20 11:58:35.200: E/Mms/media(323): at com.android.mms.model.MediaModel.<init>(MediaModel.java:77)
09-20 11:58:35.200: E/Mms/media(323): at com.android.mms.model.RegionMediaModel.<init>(RegionMediaModel.java:40)
09-20 11:58:35.200: E/Mms/media(323): at com.android.mms.model.RegionMediaModel.<init>(RegionMediaModel.java:35)
09-20 11:58:35.200: E/Mms/media(323): at com.android.mms.model.ImageModel.<init>(ImageModel.java:63)
09-20 11:58:35.200: E/Mms/media(323): at com.android.mms.data.WorkingMessage.changeMedia(WorkingMessage.java:464)
09-20 11:58:35.200: E/Mms/media(323): at com.android.mms.data.WorkingMessage.setAttachment(WorkingMessage.java:365)
09-20 11:58:35.200: E/Mms/media(323): at com.android.mms.ui.ComposeMessageActivity.addImage(ComposeMessageActivity.java:2663)
09-20 11:58:35.200: E/Mms/media(323): at com.android.mms.ui.ComposeMessageActivity.addAttachment(ComposeMessageActivity.java:2799)
09-20 11:58:35.200: E/Mms/media(323): at com.android.mms.ui.ComposeMessageActivity.handleSendIntent(ComposeMessageActivity.java:2728)
09-20 11:58:35.200: E/Mms/media(323): at com.android.mms.ui.ComposeMessageActivity.initialize(ComposeMessageActivity.java:1757)
09-20 11:58:35.200: E/Mms/media(323): at com.android.mms.ui.ComposeMessageActivity.onCreate(ComposeMessageActivity.java:1685)
09-20 11:58:35.200: E/Mms/media(323): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-20 11:58:35.200: E/Mms/media(323): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-20 11:58:35.200: E/Mms/media(323): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-20 11:58:35.200: E/Mms/media(323): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-20 11:58:35.200: E/Mms/media(323): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-20 11:58:35.200: E/Mms/media(323): at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 11:58:35.200: E/Mms/media(323): at android.os.Looper.loop(Looper.java:123)
09-20 11:58:35.200: E/Mms/media(323): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-20 11:58:35.200: E/Mms/media(323): at java.lang.reflect.Method.invokeNative(Native Method)
09-20 11:58:35.200: E/Mms/media(323): at java.lang.reflect.Method.invoke(Method.java:507)
09-20 11:58:35.200: E/Mms/media(323): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-20 11:58:35.200: E/Mms/media(323): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-20 11:58:35.200: E/Mms/media(323): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
这是我用来保存Bitmap
的一项功能。但是我并没有尝试以编程方式打开它,而是我尝试从我的文件管理器中打开它,我能够很好地查看它。修改代码供您使用:
//Global variable path
path=null;
private void SaveIamge(Bitmap finalBitmap) {
path = Environment.getExternalStorageDirectory().toString()+"\Downloads\";
String fname = "gicon.png";
File file = new File (path, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
以下是如何调用上述功能:
Bitmap bbicon;
bbicon = BitmapFactory.decodeResource(getResources(),
R.drawable.gicon);
SaveIamge(bbicon);
//use the initialized path variable to convert path to uri
try{
File fl = new File(path,"gicon.png");
Uri myURI= Uri.fromFile(fl);
/*rest of your code*/
}
catch(Exception e)
{
e.printStackTrace();
}
我希望这有助于!!!
答案 1 :(得分:2)
因为我没有在这个类中使用oncreate方法,所以在getResouces()时出现错误,所以为了清除错误,给了private Resources getResources() {
// TODO Auto-generated method stub
return null;
}
。这使得图像不会出现。
所以现在删除了上面的代码并给了bbicon = BitmapFactory.decodeResource(this.cordova.getActivity().getResources(),
解决了我的错误。