android文件选择器中的FileNotFoundException

时间:2015-05-07 11:01:44

标签: java android filenotfoundexception filechooser

我想将文件从移动存储(内部和外部存储)上传到服务器。通过文件选择器,我得到FileNotFoundException。以下是我的代码片段:

public void openFileSystem(){
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    startActivityForResult(Intent.createChooser(intent, "Select file to upload."), SELECT_FILE);
}

这是我的onActivityResult()getPath()

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == SELECT_FILE && resultCode == Activity.RESULT_OK) {

        Uri path = data.getData();
        String url = data.getData().getPath();
        File file = new File(url);
        int size = (int) file.length();
        byte[] bytes = new byte[size];
        try {
            BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
            buf.read(bytes, 0, bytes.length);
            buf.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if(cursor!=null)
        {
            int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        else return null;
    }

错误日志:

05-07 06:41:03.087: W/System.err(2195): java.io.FileNotFoundException: /document/2: open failed: ENOENT (No such file or directory)
05-07 06:41:03.097: W/System.err(2195):     at libcore.io.IoBridge.open(IoBridge.java:409)
05-07 06:41:03.097: W/System.err(2195):     at java.io.FileInputStream.<init>(FileInputStream.java:78)
05-07 06:41:03.097: W/System.err(2195):     at com.gems.ComposeBulletin.onActivityResult(ComposeBulletin.java:121)
05-07 06:41:03.117: W/System.err(2195):     at android.app.Activity.dispatchActivityResult(Activity.java:5423)
05-07 06:41:03.117: W/System.err(2195):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3361)
05-07 06:41:03.127: W/System.err(2195):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
05-07 06:41:03.137: W/System.err(2195):     at android.app.ActivityThread.access$1300(ActivityThread.java:135)
05-07 06:41:03.137: W/System.err(2195):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
05-07 06:41:03.137: W/System.err(2195):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-07 06:41:03.157: W/System.err(2195):     at android.os.Looper.loop(Looper.java:136)
05-07 06:41:03.157: W/System.err(2195):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-07 06:41:03.167: W/System.err(2195):     at java.lang.reflect.Method.invokeNative(Native Method)
05-07 06:41:03.167: W/System.err(2195):     at java.lang.reflect.Method.invoke(Method.java:515)
05-07 06:41:03.177: W/System.err(2195):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-07 06:41:03.187: W/System.err(2195):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-07 06:41:03.187: W/System.err(2195):     at dalvik.system.NativeStart.main(Native Method)
05-07 06:41:03.197: W/System.err(2195): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
05-07 06:41:03.217: W/System.err(2195):     at libcore.io.Posix.open(Native Method)
05-07 06:41:03.217: W/System.err(2195):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
05-07 06:41:03.227: W/System.err(2195):     at libcore.io.IoBridge.open(IoBridge.java:393)

所以我的座右铭是从内部存储或外部存储中选择任何文件(imagepdfdoc)并将其上传到服务器上。任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:3)

WEB-INF/classes这不可靠,因为不要求ACTION_GET_CONTENT返回指向您可以访问的文件。该文件可能位于应用程序的内部存储中,也可能位于可移动媒体上,甚至位于某个地方的云中。感觉就像本周第六次的时间,绝对是过去十分钟内的第二次...... a Uri is not a file

通过Uri上的UriContentResolver上的方法按设计使用openInputStream()

答案 1 :(得分:0)

试试这个......

 Browse = (ImageView)findViewById(R.id.browsehoro);
    Browse.setOnClickListener(this); 

 public void onClick(View v)
{

    switch(v.getId())
    {

    case R.id.browsehoro:
        img.setEnabled(true);
        Intent intent = new Intent(Horoscope.this, FilePickerHoroscope.class);            
        startActivityForResult(intent, REQUEST_PICK_FILE);
        break;
    }


}

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{

    if (resultCode == RESULT_OK) {
       /* if (requestCode == REQUEST_PICK_FILE) {
            Uri selectedImageUri = data.getData();
            selectedFile = getPath(selectedImageUri);

 //                Print imagepath in textview here
            filePath.setText(selectedFile);
            System.out.println(requestCode);
            System.out.println("Image Path : " + selectedFile);
            img.setImageURI(selectedImageUri);


        }*/


        switch(requestCode) 
        {

        case REQUEST_PICK_FILE:

            if(data.hasExtra(FilePickerHoroscope.EXTRA_FILE_PATH))
             {
                selectedFile = new String
                        (data.getStringExtra(FilePickerHoroscope.EXTRA_FILE_PATH));
                filePath.setText(selectedFile.toString());  
            }
            break;
        }
    }
}

答案 2 :(得分:-1)

String url = data.getData().getPath();
File file = new File(url);

String url包含内容提供者路径。在这种情况下,您可以在logcat /document/2中看到。 File类无法处理此类内容提供程序路径。

首先必须将该路径转换为实际的文件系统路径。

谷歌的'getRealPathFromUri'。