簡體   English   中英

android file picker中的FileNotFoundException

[英]FileNotFoundException in android file picker

我想將文件從移動存儲(內部和外部存儲)上傳到服務器。通過使用文件選擇器,我得到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 )並將其上傳到服務器上。 任何幫助將不勝感激!

使用ACTION_GET_CONTENT這是不可靠的,因為不要求Uri返回指向您可以訪問的文件。 該文件可能位於應用程序的內部存儲中,或位於可移動媒體上,甚至位於某處的雲中。 對於本周第六次,而且絕對是過去十分鍾中的第二 ,感覺…… Uri不是文件

通過ContentResolver上的方法openInputStream()openInputStream()getType()使用設計的Uri

嘗試這個...

 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;
        }
    }
}
String url = data.getData().getPath();
File file = new File(url);

字符串url確實包含內容提供者路徑。 在這種情況下,您可以在logcat /document/2看到。 File類無法處理此類內容提供程序路徑。

您首先必須將該路徑轉換為真實的文件系統路徑。

Google的“ getRealPathFromUri”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM