【问题标题】:"Invalid File" error when the images in sdcard are clicked单击 sdcard 中的图像时出现“无效文件”错误
【发布时间】:2013-11-07 11:55:06
【问题描述】:

要通过电子邮件和 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;
    }
}

【问题讨论】:

  • 尝试 file:///... 文件路径而不是 Uri.fromFile(r1),我不太确定它在里面做了什么。

标签: java android email-attachments android-sdcard


【解决方案1】:
            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-09-25
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 2017-09-07
    相关资源
    最近更新 更多