2014-10-20 31 views
1

文件路徑= /外部/圖像/媒體/ 2如何刪除FileNotFoundException:android中的異常?

獲取錯誤: java.io.FileNotFoundException:/外部/圖像/媒體/ 2(沒有這樣的文件或目錄)

您好我得到這個錯誤 java.io.FileNotFoundException:/ external/images/media/2(沒有這樣的文件或目錄)。其實我正在模擬器上檢查這個。首先我從圖庫 中選擇圖像,然後獲取所選圖像的字節。但我沒有找到錯誤文件爲什麼

這裏是我的代碼:

package com.CA.xmlparsing; 

import java.io.BufferedInputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Toast; 


public class MainActivity extends Activity { 

    private static final int PICKFILE_RESULT_CODE = 2; 
    private String FilePath; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    public void upLoadImage(View view) { 

     Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setType("*/*"); 
    intent.addCategory(Intent.CATEGORY_OPENABLE); 

    try { 
     startActivityForResult(
       Intent.createChooser(intent, "Select a File to Upload"), 
       PICKFILE_RESULT_CODE); 
    } catch (android.content.ActivityNotFoundException ex) { 
     // Potentially direct the user to the Market with a Dialog 
     Toast.makeText(this, "Please install a File Manager.", 
       Toast.LENGTH_SHORT).show(); 
    } 

    } protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // TODO Fix no activity available 
     if (data == null) 
      return; 
     switch (requestCode) { 
     case PICKFILE_RESULT_CODE: 
      if (resultCode == RESULT_OK) { 
       FilePath = data.getData().getPath(); 
       //FilePath is your file as a string 
       Log.d("--------------", FilePath); 
// print /external/images/media/2 
       byte[] bytes= getFileByte(FilePath); 
       Log.d("--------------", ""+bytes); 
      } 
     } 
    } 

    private byte [] getFileByte(String path){ 
     File file = new File(path); 
      int size = (int) file.length(); 
      byte[] bytes = new byte[size]; 
      try { 
// getting error on this line 
       BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file)); 
       buf.read(bytes, 0, bytes.length); 
       buf.close(); 
      } catch (FileNotFoundException e) { 
//catch here the error 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return bytes; 
    } 
} 


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.CA.xmlparsing.MainActivity" > 

<Button 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="uploadImage" 
    android:onClick="upLoadImage" /> 

</RelativeLayout> 

有從仿真器讀取圖像所需塔爾任何許可?

+0

什麼線是從哪裏來的錯誤? – AmaJayJB 2014-10-20 07:58:07

+0

你可以從答案[這裏複製] [1] [1]:http://stackoverflow.com/a/25467200/3322203 – 2014-10-20 08:01:24

回答

0

有權限,您需要添加到您的Android清單,雖然這可能不是你唯一的問題。地址:

<manifest ...> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
... 

1

你得到的不是文件系統,但通過mediastore等使用的媒體路徑的文件路徑。爲了獲得真正的文件路徑,您必須在媒體商店上調用光標。你可以在這個網站找到很多示例代碼。

+0

@greeenapp可以請您給一些代碼 – user944513 2014-10-20 09:13:18

+0

我們再也不需要因爲另一個有很大數字的用戶已經發布了這個消息 – greenapps 2014-10-20 10:37:04

1

你真正的問題是將圖像轉換爲字節,因此你可以上傳到服務器上。 onActivityResult方法將爲您提供一個Bitmap對象。

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 
     Bitmap bmp = (Bitmap) data.getExtras().get("data"); 
    } 
} 

現在不是得到路徑,然後將其轉化爲文件,然後再次將其轉換爲字節,您可以直接使用您的onActivityResult方法獲取位圖對象,並使用以下方法將其轉換爲字節。 ,因此要獲得圖像的路徑,你需要:

public byte[] bitmapToByte(Bitmap bitmap){ 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 
    byte[] byteArray = stream.toByteArray(); 
    return byteArray; 
} 

但是這個職位要求「在android系統異常如何刪除FileNotFoundException異常?」:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 
     Bitmap photo = (Bitmap) data.getExtras().get("data"); 

     // CALL THIS METHOD TO GET THE URI FROM THE BITMAP 
     Uri tempUri = getImageUri(getApplicationContext(), photo); 

     // CALL THIS METHOD TO GET THE ACTUAL PATH 
     File finalFile = new File(getAbsolutePathFromURI(tempUri)); 
    } 
} 

public Uri getImageUri(Context context, Bitmap bitmap) { 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
    String path = Images.Media.insertImage(context.getContentResolver(), bitmap, "TempImage", null); 
    return Uri.parse(path); 
} 

public String getAbsolutePathFromURI(Uri uri) { 
    Cursor cursor = getContentResolver().query(uri, null, null, null, null); 
    cursor.moveToFirst(); 
    int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
    return cursor.getString(index); 
}