Вопрос

I need to send MMS in android so had given the code as below, the images from drawable is first saved in sd card then fetched back to the SMS body, but after clicking "SEND" button i get the image as shown here. the saved images in downloads from sd card are not openable, i get error as "invalid file". checked with the image name everything is in lowercase alphabets. where am i going wrong please let me know.

enter image description here

Intent code

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 Error

 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)
Это было полезно?

Решение 2

since i'm not using oncreate method in this class so was getting error at getResouces() so had given private Resources getResources() { // TODO Auto-generated method stub return null; } in order to clear the error. and this was making the image not to appear. so now deleted the above code and gave bbicon = BitmapFactory.decodeResource(this.cordova.getActivity().getResources(), which solved my error.

Другие советы

This is one function that i used for saving Bitmap. However i didn't try opening it programatically, rather i tried opening it from my file manager and i was able to view it fine. Modifying the code for your use:

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

Below is how you can call the above function:

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

I hope this Helps!!!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top