【问题标题】:android - getpath method returns incorrect addressandroid - getpath 方法返回不正确的地址
【发布时间】:2013-09-15 04:20:39
【问题描述】:

我使用startActivityForResult 从图库中挑选一张图片,然后使用onActivityResult 带来结果。 当我使用getPath() 作为意图结果将路径发送到另一个活动以设置该图片的来源时,路径不正确并且是图片不存在的另一种方式。

图片位于sdcard中,位于:“mnt/sdcard/pictures/lambo”----但getpath()他的返回:“external/images/media/17”

照片选择器:

private void photopicker() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PHOTO);
}

onActivityResult :

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
        switch(requestCode) {
        case SELECT_PHOTO:
            if(resultCode == RESULT_OK){  
                Uri selectedImage = imageReturnedIntent.getData();
                String add;
                add = selectedImage.getPath();   // don't work
                InputStream imageStream = null;
                try {
                    imageStream = getContentResolver().openInputStream(selectedImage);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                selectedPhoto = BitmapFactory.decodeStream(imageStream);
//              add = selectedImage.getPath(); //  don't work
                Intent intent = new Intent(MainActivity.this, PicViewer.class);
                intent.putExtra("add", add);
                startActivity(intent);
            }
        }
    }

【问题讨论】:

    标签: android android-intent path gallery syntax-error


    【解决方案1】:

    你可以试试这个:

    public String getRealPathFromURI(Uri contentUri) {
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    

    【讨论】:

      猜你喜欢
      • 2013-06-06
      • 1970-01-01
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 2018-11-20
      • 1970-01-01
      • 2018-03-09
      • 2016-12-17
      相关资源
      最近更新 更多