我只需要将文件中的路径转换为InputStream,这里是我的代码。我通过调用Intent选择路径,但它总是给我filenotfoundexception
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_FROM_FILE && resultCode == RESULT_OK) {
File file = new File(Environment.getExternalStorageDirectory(), data.getData().toString());
Uri path = Uri.fromFile(file);
File fpath=new File(path.toString());
try {
InputStream myfile=new FileInputStream(fpath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
提前感谢您的帮助......
答案 0 :(得分:2)
试试这个:
Uri uri = data.getData();
ContentResolver cr = this.getContentResolver();
try {
InputStream myfile=cr.openInputStream(uri));;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
答案 1 :(得分:0)
Intent intent = new Intent();
intent.setType("image/*");
//intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == getActivity(). RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();
try {
//Getting the Bitmap from Gallery
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
//Setting the Bitmap to ImageView
Glide.with(getActivity()).load(filePath).asBitmap().placeholder(R.drawable.ic_launcher).into(avatarContainer);
//avatarContainer.setImageBitmap(bitmap);
//uploadFile(filePath.getPath());
try {
uploadFile(getPath(filePath));
} catch (Exception e) {
Config.displayToast("Please Select Image From Gallary");
alertDialog.dismiss();
ServerRequest.dismissDialogBox(context);
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}else if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE && resultCode == getActivity() .RESULT_OK){
String filepath = fileUri.getPath();
Log.d("Filepath is",""+filepath);
BitmapFactory.Options options = new BitmapFactory.Options();
// down sizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 8;
final Bitmap bitmap = BitmapFactory.decodeFile(filepath, options);
//avatarContainer.setImageBitmap(bitmap);
Glide.with(getActivity()).load(filepath).asBitmap().placeholder(R.drawable.ic_launcher).into(avatarContainer);
uploadFile(filepath);
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
@SuppressWarnings("deprecation")
Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}