问题描述
文件路径= /外部/图像/媒体/ 2
获取错误:
java.io.FileNotFoundException:/外部/图像/媒体/ 2(没有这样的文件或目录)
喜
我特林上传服务器选择的图像。我正在此错误
java.io.FileNotFoundException:/外部/图像/媒体/ 2(没有这样的文件或目录)。其实我检查这对我simulator.First选择图库图片
然后得到选择image.but的我收到未找到错误的文件,为什么字节
HiI am tring to upload the selected image on server .I am getting this errorjava.io.FileNotFoundException: /external/images/media/2 (No such file or directory).Actually I am checking this on simulator.First I select the image from gallerythen get the bytes of selected image.but I am getting error file not found why
这是我的code:
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>
有从仿真器读取图像塔尔所需的许可?
is there any permission required for thar to read image from emulator ?
推荐答案
你得到的不是文件系统,但通过mediastore等人利用媒体路径的文件路径。为了得到一个真正的文件路径,你必须调用媒体商店光标。你可以找到在这个网站的许多例子codeS。
What you get is not a file path for the file system but a media path in use by mediastore and others. In order to get a real filepath you have to invoke a cursor on the media store. You can find many example codes on this site.
这篇关于在android系统异常:如何删除FileNotFoundException异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!