无法从图库中选择照片?

发布于 2024-10-15 05:30:00 字数 2618 浏览 2 评论 0原文

我能够打开画廊并获取画廊的路径为= 内容://媒体/外部/图像/媒体/2 但无法在 imageview 中解码 这是我的代码

public void onCreate(Bundle savingInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

            b=(Button) findViewById(R.id.Button01);
            b.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {

                    // in onCreate or any event where your want the user to
                    // select a file
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(intent,IMAGE_PICK);
                }
            });
        }

        //UPDATED
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == RESULT_OK) {
                if (requestCode == IMAGE_PICK) {
                    Uri selectedImageUri = data.getData();


                    //OI FILE Manager
                    filemanagerstring = selectedImageUri.getPath();

                    //MEDIA GALLERY
                    selectedImagePath = getPath(selectedImageUri);




                    if(selectedImagePath!=null)
                    {
                        path = selectedImageUri.toString();
                         m1.setPath(path);


                          BitmapFactory.Options options = new BitmapFactory.Options();
                          options.inSampleSize = 4;

                             Bitmap yourSelectedImage = BitmapFactory.decodeFile(path, options);
                             ImageButton img2=(ImageButton)findViewById(R.id.widget27);
                             img2.setImageBitmap(yourSelectedImage);
                             Toast.makeText(getBaseContext(), path, 1000).show();

                           }



                    }
            }
        }
        public String getPath(Uri uri) {





        String [] proj={MediaStore.Images.Media.DATA};   
        Cursor cursor = managedQuery(uri,   
                proj, // Which columns to return   
                null,       // WHERE clause; which rows to return (all rows)   
                null,       // WHERE clause selection arguments (none)   
                null); // Order-by clause (ascending by name)   
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);   
        cursor.moveToFirst();   

        return cursor.getString(column_index);   

} 请

帮忙提前致谢

I am able to open the gallery and getting the path of gallery as=
content://media/external/images/media/2
but not able to decode in imageview
this is my code

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

            b=(Button) findViewById(R.id.Button01);
            b.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {

                    // in onCreate or any event where your want the user to
                    // select a file
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(intent,IMAGE_PICK);
                }
            });
        }

        //UPDATED
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == RESULT_OK) {
                if (requestCode == IMAGE_PICK) {
                    Uri selectedImageUri = data.getData();


                    //OI FILE Manager
                    filemanagerstring = selectedImageUri.getPath();

                    //MEDIA GALLERY
                    selectedImagePath = getPath(selectedImageUri);




                    if(selectedImagePath!=null)
                    {
                        path = selectedImageUri.toString();
                         m1.setPath(path);


                          BitmapFactory.Options options = new BitmapFactory.Options();
                          options.inSampleSize = 4;

                             Bitmap yourSelectedImage = BitmapFactory.decodeFile(path, options);
                             ImageButton img2=(ImageButton)findViewById(R.id.widget27);
                             img2.setImageBitmap(yourSelectedImage);
                             Toast.makeText(getBaseContext(), path, 1000).show();

                           }



                    }
            }
        }
        public String getPath(Uri uri) {





        String [] proj={MediaStore.Images.Media.DATA};   
        Cursor cursor = managedQuery(uri,   
                proj, // Which columns to return   
                null,       // WHERE clause; which rows to return (all rows)   
                null,       // WHERE clause selection arguments (none)   
                null); // Order-by clause (ascending by name)   
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);   
        cursor.moveToFirst();   

        return cursor.getString(column_index);   

}
}

Please help thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

机场等船 2024-10-22 05:30:00

这段代码应该放在你的 onActivityResult 中。它为您提供了从获取的图像 URI 中解码文件路径的方法:

            Uri selectedImageUri = data.getData();
            String[] projection = { MediaStore.Images.Media.DATA}; 
            Cursor cursor = managedQuery(selectedImageUri, projection, null, null, null); 
            int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst(); 
            selectedImagePath = cursor.getString(column_index_data);
            Bitmap galleryImage = BitmapFactory.decodeFile(selectedImagePath);

This piece of code should be put in your onActivityResult. It gives you the way to decode file path from fetched image URI:

            Uri selectedImageUri = data.getData();
            String[] projection = { MediaStore.Images.Media.DATA}; 
            Cursor cursor = managedQuery(selectedImageUri, projection, null, null, null); 
            int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst(); 
            selectedImagePath = cursor.getString(column_index_data);
            Bitmap galleryImage = BitmapFactory.decodeFile(selectedImagePath);
旧故 2024-10-22 05:30:00

我们需要对早期 onActivityResult() 的图库选择器代码进行以下更改/修复,以便在 Kitkat 和所有其他早期版本上无缝运行。

Uri selectedImgFileUri = data.getData();

if (selectedImgFileUri == null ) {

// user has not selected any photo

}

try {

InputStream input = mActivity.getContentResolver().openInputStream(selectedImgFileUri);

mSelectedPhotoBmp = BitmapFactory.decodeStream(input);

} catch (Throwable tr) {

// 显示重试消息

}

We need to do following changes/fixes in our earlier onActivityResult()'s gallery picker code to run seamlessly on Kitkat and on all other earlier versions as well.

Uri selectedImgFileUri = data.getData();

if (selectedImgFileUri == null ) {

// user has not selected any photo

}

try {

InputStream input = mActivity.getContentResolver().openInputStream(selectedImgFileUri);

mSelectedPhotoBmp = BitmapFactory.decodeStream(input);

} catch (Throwable tr) {

// show message to try again

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文