Java 使用android内容的FileInputStream
我正在尝试从android设备上的图库访问图像,但我收到的图库图片路径始终显示此错误:Java 使用android内容的FileInputStream,java,android,Java,Android,我正在尝试从android设备上的图库访问图像,但我收到的图库图片路径始终显示此错误: java.io.FileNotFoundException: /external/images/media/949: open failed: ENOENT (No such file or directory) 这不是一个文件路径。它是从ContentProvider获取的Uri的路径部分。要在其上获取InputStream,请在ContentResolver上使用openInputStream()。通过
java.io.FileNotFoundException: /external/images/media/949: open failed: ENOENT (No such file or directory)
这不是一个文件路径。它是从
ContentProvider
获取的Uri
的路径部分。要在其上获取InputStream
,请在ContentResolver
上使用openInputStream()
。通过在活动
上调用getContentResolver()
或其他非文件路径的上下文
,可以获取ContentResolver
。它是从ContentProvider
获取的Uri
的路径部分。要在其上获取InputStream
,请在ContentResolver
上使用openInputStream()
。通过在活动
上调用getContentResolver()
或其他非文件路径的上下文
,可以获取ContentResolver
。它是从ContentProvider
获取的Uri
的路径部分。要在其上获取InputStream
,请在ContentResolver
上使用openInputStream()
。通过在活动
上调用getContentResolver()
或其他非文件路径的上下文
,可以获取ContentResolver
。它是从ContentProvider
获取的Uri
的路径部分。要在其上获取InputStream
,请在ContentResolver
上使用openInputStream()
。您可以通过在活动中调用getContentResolver()
或其他上下文中调用ContentResolver
来获取ContentResolver,只需扩展Commonware的答案即可
以下是我在一些应用程序中使用的代码示例:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode) {
case PICTURE_GALLERY:
if (resultCode == RESULT_OK && intent != null) {
Uri selectedImage = intent.getData();
final String[] filePathColumn = { MediaColumns.DATA, MediaColumns.DISPLAY_NAME };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
// some devices (OS versions return an URI of com.android instead of com.google.android
if (selectedImage.toString().startsWith("content://com.android.gallery3d.provider")) {
// use the com.google provider, not the com.android provider.
selectedImage = Uri.parse(selectedImage.toString().replace("com.android.gallery3d","com.google.android.gallery3d"));
}
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(MediaColumns.DATA);
// if it is a picasa image on newer devices with OS 3.0 and up
if (selectedImage.toString().startsWith("content://com.google.android.gallery3d")){
columnIndex = cursor.getColumnIndex(MediaColumns.DISPLAY_NAME);
if (columnIndex != -1) {
progress_bar.setVisibility(View.VISIBLE);
final Uri uriurl = selectedImage;
// Do this in a background thread, since we are fetching a large image from the web
new Thread(new Runnable() {
public void run() {
Bitmap the_image = getBitmap("image_file_name.jpg", uriurl);
}
}).start();
}
} else { // it is a regular local image file
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap the_image = decodeFile(new File(filePath));
}
}
// If it is a picasa image on devices running OS prior to 3.0
else if (selectedImage != null && selectedImage.toString().length() > 0) {
progress_bar.setVisibility(View.VISIBLE);
final Uri uriurl = selectedImage;
// Do this in a background thread, since we are fetching a large image from the web
new Thread(new Runnable() {
public void run() {
Bitmap the_image = getBitmap("image_file_name.jpg", uriurl);
}
}).start();
}
}
break;
}
}
只是扩展了Commonware的答案
以下是我在一些应用程序中使用的代码示例:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode) {
case PICTURE_GALLERY:
if (resultCode == RESULT_OK && intent != null) {
Uri selectedImage = intent.getData();
final String[] filePathColumn = { MediaColumns.DATA, MediaColumns.DISPLAY_NAME };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
// some devices (OS versions return an URI of com.android instead of com.google.android
if (selectedImage.toString().startsWith("content://com.android.gallery3d.provider")) {
// use the com.google provider, not the com.android provider.
selectedImage = Uri.parse(selectedImage.toString().replace("com.android.gallery3d","com.google.android.gallery3d"));
}
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(MediaColumns.DATA);
// if it is a picasa image on newer devices with OS 3.0 and up
if (selectedImage.toString().startsWith("content://com.google.android.gallery3d")){
columnIndex = cursor.getColumnIndex(MediaColumns.DISPLAY_NAME);
if (columnIndex != -1) {
progress_bar.setVisibility(View.VISIBLE);
final Uri uriurl = selectedImage;
// Do this in a background thread, since we are fetching a large image from the web
new Thread(new Runnable() {
public void run() {
Bitmap the_image = getBitmap("image_file_name.jpg", uriurl);
}
}).start();
}
} else { // it is a regular local image file
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap the_image = decodeFile(new File(filePath));
}
}
// If it is a picasa image on devices running OS prior to 3.0
else if (selectedImage != null && selectedImage.toString().length() > 0) {
progress_bar.setVisibility(View.VISIBLE);
final Uri uriurl = selectedImage;
// Do this in a background thread, since we are fetching a large image from the web
new Thread(new Runnable() {
public void run() {
Bitmap the_image = getBitmap("image_file_name.jpg", uriurl);
}
}).start();
}
}
break;
}
}
只是扩展了Commonware的答案
以下是我在一些应用程序中使用的代码示例:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode) {
case PICTURE_GALLERY:
if (resultCode == RESULT_OK && intent != null) {
Uri selectedImage = intent.getData();
final String[] filePathColumn = { MediaColumns.DATA, MediaColumns.DISPLAY_NAME };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
// some devices (OS versions return an URI of com.android instead of com.google.android
if (selectedImage.toString().startsWith("content://com.android.gallery3d.provider")) {
// use the com.google provider, not the com.android provider.
selectedImage = Uri.parse(selectedImage.toString().replace("com.android.gallery3d","com.google.android.gallery3d"));
}
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(MediaColumns.DATA);
// if it is a picasa image on newer devices with OS 3.0 and up
if (selectedImage.toString().startsWith("content://com.google.android.gallery3d")){
columnIndex = cursor.getColumnIndex(MediaColumns.DISPLAY_NAME);
if (columnIndex != -1) {
progress_bar.setVisibility(View.VISIBLE);
final Uri uriurl = selectedImage;
// Do this in a background thread, since we are fetching a large image from the web
new Thread(new Runnable() {
public void run() {
Bitmap the_image = getBitmap("image_file_name.jpg", uriurl);
}
}).start();
}
} else { // it is a regular local image file
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap the_image = decodeFile(new File(filePath));
}
}
// If it is a picasa image on devices running OS prior to 3.0
else if (selectedImage != null && selectedImage.toString().length() > 0) {
progress_bar.setVisibility(View.VISIBLE);
final Uri uriurl = selectedImage;
// Do this in a background thread, since we are fetching a large image from the web
new Thread(new Runnable() {
public void run() {
Bitmap the_image = getBitmap("image_file_name.jpg", uriurl);
}
}).start();
}
}
break;
}
}
只是扩展了Commonware的答案
以下是我在一些应用程序中使用的代码示例:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode) {
case PICTURE_GALLERY:
if (resultCode == RESULT_OK && intent != null) {
Uri selectedImage = intent.getData();
final String[] filePathColumn = { MediaColumns.DATA, MediaColumns.DISPLAY_NAME };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
// some devices (OS versions return an URI of com.android instead of com.google.android
if (selectedImage.toString().startsWith("content://com.android.gallery3d.provider")) {
// use the com.google provider, not the com.android provider.
selectedImage = Uri.parse(selectedImage.toString().replace("com.android.gallery3d","com.google.android.gallery3d"));
}
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(MediaColumns.DATA);
// if it is a picasa image on newer devices with OS 3.0 and up
if (selectedImage.toString().startsWith("content://com.google.android.gallery3d")){
columnIndex = cursor.getColumnIndex(MediaColumns.DISPLAY_NAME);
if (columnIndex != -1) {
progress_bar.setVisibility(View.VISIBLE);
final Uri uriurl = selectedImage;
// Do this in a background thread, since we are fetching a large image from the web
new Thread(new Runnable() {
public void run() {
Bitmap the_image = getBitmap("image_file_name.jpg", uriurl);
}
}).start();
}
} else { // it is a regular local image file
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap the_image = decodeFile(new File(filePath));
}
}
// If it is a picasa image on devices running OS prior to 3.0
else if (selectedImage != null && selectedImage.toString().length() > 0) {
progress_bar.setVisibility(View.VISIBLE);
final Uri uriurl = selectedImage;
// Do this in a background thread, since we are fetching a large image from the web
new Thread(new Runnable() {
public void run() {
Bitmap the_image = getBitmap("image_file_name.jpg", uriurl);
}
}).start();
}
}
break;
}
}