文件路徑= /外部/圖像/媒體/ 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>
有從仿真器讀取圖像所需塔爾任何許可?
什麼線是從哪裏來的錯誤? – AmaJayJB 2014-10-20 07:58:07
你可以從答案[這裏複製] [1] [1]:http://stackoverflow.com/a/25467200/3322203 – 2014-10-20 08:01:24