FilePath = / external / images / media / 2
出现错误:
java.io.FileNotFoundException:/ external / images / media / 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>
thar是否需要任何许可才能从模拟器读取图像?
最佳答案
您得到的不是文件系统的文件路径,而是mediastore等使用的媒体路径。为了获得真实的文件路径,您必须在媒体存储上调用游标。您可以在此站点上找到许多示例代码。
关于android - 如何删除FileNotFoundException:Android中的异常?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26460842/