0

要通过电子邮件和 mms 共享图像,第一步我需要将图像保存在 sdcard 中,但对我来说,保存的图像没有打开,而是出现“无效文件”错误,我检查了扩展格式,一切都正确但不知道在哪里我错了。

下面是java代码。

public class Share extends CordovaPlugin {

    public static final String ACTION_POSITION = "ShareImage";

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
            throws JSONException {
        if (ACTION_POSITION.equals(action)) {
            try {
                JSONObject arg_object = args.getJSONObject(0);
                Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
                sendIntent.setType("image/jpg");
                sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, arg_object.getString("image"));
                String name = arg_object.getString("image");
                String defType = "drawable";
                String defPackage = "com.picsswipe";
                int drawableId = this.cordova.getActivity().getResources().getIdentifier( name , defType, defPackage );

              //  Bitmap bbicon = BitmapFactory.decodeFile( arg_object.getString("image") );
                Bitmap bbicon = BitmapFactory.decodeResource( this.cordova.getActivity().getResources(),drawableId  );

                 String extStorageDirectory = Environment.getExternalStorageDirectory().toString();         
                OutputStream outStream = null;              

                 File f = new File(extStorageDirectory + "/Download/",
                         "jj.jpg" );
                 try {
                        outStream = new FileOutputStream(f); 
                         bbicon.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
                        outStream.flush();
                        outStream.close();              
                    } catch (Exception e) {
                    }                       

                File r1 = new File(Environment.getExternalStorageDirectory()
                        .getAbsolutePath() + "/Download/", "jj.jpg");

               //RETRIEVING IMAGES FROM SDCARD                         

                Uri uri1 = Uri.fromFile(r1);    
                sendIntent.putExtra(Intent.EXTRA_STREAM, uri1);      
                sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(r1));                                 
                 Uri uris = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "jj.jpg"));                            
                this.cordova.getActivity().startActivity(sendIntent);
            } catch (Exception e) {
                System.err.println("Exception: " + e.getMessage());
                callbackContext.error(e.getMessage());
                return false;
            }
        }
        return true;
    }
}
4

1 回答 1

0
            File file;
            File rootPath = android.os.Environment
                    .getExternalStorageDirectory();
            File directory = new File(rootPath.getAbsolutePath()
                    + "/Download");
            if (!directory.exists())
                directory.mkdir();
            file = new File(directory, "filename.PNG");//.png/.jpg anything you want        
            try {
                Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.pincheck);
                FileOutputStream outStream;
                outStream = new FileOutputStream(file);
                bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);             
                outStream.flush();
                outStream.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

并且您应该在清单文件中添加此权限。然后只有文件将复制到您的外部 sd 卡。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
于 2013-11-07T12:14:10.917 回答