3

我需要在 android 中发送彩信,所以给出了如下代码,drawable 中的图像首先保存在 sd 卡中,然后取回 SMS 正文,但是在单击“发送”按钮后,我得到了如下所示的图像。从 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)
4

2 回答 2

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();
}

我希望这有帮助!!!

于 2013-09-20T06:52:07.213 回答
2

由于我没有在此类中使用 oncreate 方法,因此在 getResouces() 处出现错误,因此已给出private Resources getResources() { // TODO Auto-generated method stub return null; }以清除错误。这使图像不出现。所以现在删除了上面的代码并给出了bbicon = BitmapFactory.decodeResource(this.cordova.getActivity().getResources(),解决我的错误。

于 2013-09-20T12:27:14.447 回答