0

我想从我的应用程序上传图像、音频和视频文件。如果我使用图库 ( content://media/external/images/media/3163) 选择图像,它可以正常工作,但如果我选择使用文件管理器 ( file:///storage/sdcard0/blue.jpg),它就不起作用。

uri=intent.getData();
String[] filePathColumn = { MediaStore.Images.ImageColumns.DATA };
if (uri != null) {
     Cursor cursor = RootActivity.rootContext.getContentResolver().query(Uri.parse(uri),filePathColumn, null, null, null);
}

如果光标返回 null uri = file:///storage/sdcard0/blue.jpg,有人可以帮助我吗?

4

2 回答 2

2

尝试如下:

button.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(
                    Intent.createChooser(intent, "Select Picture"),
                    SELECT_PICTURE);
        }
    });

并在您的活动中添加两种方法

  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {

            Uri selectedImageUri = data.getData();
            String s1 = data.getDataString();
            //String s1 = selectedImageUri.getPath();
            Log.e("GetPath",s1);
            Log.e("OK",""+selectedImageUri);

            selectedImagePath = getPath(selectedImageUri);
            if(selectedImagePath==null && s1 != null)
            {
                selectedImagePath = s1.replaceAll("file://","");
            }

            Intent intent = new Intent(this, PhotoEditorActivity.class);
            intent.putExtra("path", selectedImagePath);
            startActivity(intent);
            finish();

        }
                  }
               }


//Return the path of the file.
   public String getPath(Uri uri) {

    try{
    String[] projection = { MediaStore.Images.Media.DATA };
    Log.e("OK 1",""+projection);
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    Log.e("OK 2",""+cursor);
    if(cursor==null)
    {
        return null;

    }
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    Log.e("OK 3",""+column_index);
    cursor.moveToFirst();
    Log.e("OK 4",""+cursor.getString(column_index));
    return cursor.getString(column_index);
    }
    catch(Exception e)
    {
        Toast.makeText(PhotoActivity.this, "Image is too big in resolution please try again", 5).show();
        return null;
    }

}

并将这个 int 添加为类成员

  private static final int SELECT_PICTURE = 1;
于 2013-02-01T06:57:36.933 回答
0

试试这个代码:

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-02-01T06:12:47.773 回答