Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用android内容的FileInputStream_Java_Android - Fatal编程技术网

Java 使用android内容的FileInputStream

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()。通过

我正在尝试从android设备上的图库访问图像,但我收到的图库图片路径始终显示此错误:

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;
    }
}