0

我正在尝试将 /mnt/sdcard/img.jpg 中的图像读入 ImageView。

File image = new File(path);
          Bitmap bm = BitmapFactory.decodeFile(image.getAbsolutePath());
          webView1.setImageBitmap(bm); 

我从图库中获取路径,我的应用程序说该路径不存在。但是在文件管理器中它是存在的。

我该如何解决?

if (arg0 == button4){
            Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, 1);

        }

我如何获取图像文件的路径:

private String UriToBit(Uri uri) {
        String[] proj = {MediaStore.Images.Media.DATA};
        Cursor cursor = managedQuery(uri, proj, null, null, null);
        int index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(index);
    }
4

1 回答 1

0

嗨,下面的代码我用于将 sdcard 拾取的图像显示到 imageview 中。

selectimage = (Button) findViewById(R.id.selectimg);

    selectimage.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             Intent i = new  Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                  startActivityForResult(i, PICK_FROM_FILE);                

            }
        }
    });

   protected void onActivityResult(int requestCode, int resultCode, Intent data) {

           if  (resultCode == RESULT_OK) {


        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA  };

        Cursor cursor = getContentResolver().query(selectedImage,
                                   filePathColumn,null,null, null);
        cursor.moveToFirst();

        int  columnIndex = cursor.getColumnIndexfilePathColumn[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();

        yourSelectedImage  = BitmapFactory.decodeFile(filePath);
        img.setImageBitmap(yourSelectedImage);
于 2012-10-10T08:47:29.027 回答