繁体   English   中英

Android getAbsolutePath(data.getData())始终返回null

[英]Android getAbsolutePath(data.getData()) always returns null

我正在尝试编写一个应用来显示用户选择的图像的EXIF数据(学校已经结束,我很无聊),在寻找如何允许用户选择图像时,在线的普遍共识似乎是:要么拥有文件浏览器,或使用意图启动另一个可以查看文件并收集返回数据的应用程序。

我发现以下代码用于此公共类ListFileActivity扩展了ActionBarActivity {

private String selectedImagePath = "";
final private int PICK_IMAGE = 1;
final private int CAPTURE_IMAGE = 2;
ImageView imgView;
private String imgPath;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_file);

}

public void cameraClick(View view) {
    final Intent intent = new Intent(
            MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
            setImageUri());
    startActivityForResult(intent, CAPTURE_IMAGE);
}
public void galleryClick(View view) {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(
            Intent.createChooser(intent, ""),
            PICK_IMAGE);
}

public Uri setImageUri() {
    // Store image in dcim
    File file = new File(Environment.getExternalStorageDirectory()
            + "/DCIM/", "image" + new Date().getTime() + ".png");
    Uri imgUri = Uri.fromFile(file);
    this.imgPath = file.getAbsolutePath();
    return imgUri;
}

public String getImagePath() {
    return imgPath;
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent data) {
    if (resultCode != Activity.RESULT_CANCELED) {
        if (requestCode == PICK_IMAGE) {
            selectedImagePath = getAbsolutePath(data.getData());
            String TAG = "Showing image";
            Log.v(TAG, "Showing the image with a path of " + selectedImagePath);
            //The path returns null, piece of crap

            //imgView.setImageBitmap(decodeFile(selectedImagePath));
        } else if (requestCode == CAPTURE_IMAGE) {
            selectedImagePath = getImagePath();
            imgView.setImageBitmap(decodeFile(selectedImagePath));
        } else {
            super.onActivityResult(requestCode, resultCode,
                    data);
        }
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is
    // present.
    getMenuInflater().inflate(R.menu.menu_main, menu); //Was originally R.menu.main
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public Bitmap decodeFile(String path) {
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, o);
        // The new size we want to scale to
        final int REQUIRED_SIZE = 70;

        // Find the correct scale value. It should be the power of
        // 2.
        int scale = 1;
        while (o.outWidth / scale / 2 >= REQUIRED_SIZE
                && o.outHeight / scale / 2 >= REQUIRED_SIZE)
            scale *= 2;

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeFile(path, o2);
    } catch (Throwable e) {
        e.printStackTrace();
    }
    return null;

}

public String getAbsolutePath(Uri uri) {
    String[] projection = { MediaColumns.DATA };
    @SuppressWarnings("deprecation")
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    if (cursor != null) {
        int column_index = cursor
                .getColumnIndexOrThrow(MediaColumns.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } else
        return null;
}


}`

此代码有许多不同的变体,所有变体都几乎相同,我发现了这一变体,因为它允许使用相机。

问题是,无论我尝试使用哪个代码段,选择或拍摄图像后返回的目录始终为空。 这在模拟器和我的手机中的各种目录中均会发生。

该应用具有所需的权限-

<uses-permission android:name="ANDROID.PERMISSION.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE"/>

当返回null时,getAbsolutePath(Uri uri)中的游标必须为null,因为这是返回null的时候,但是我不明白为什么。

感谢所有帮助

编辑-我已经添加了更多的日志标签,并且光标似乎正在传递if(cursor != null) {}这是return cursor.getString(column_index) ,它返回null

编辑2-整数column_index,即cursor.getColumnIndexOrThrow(MediaColumns.DATA)返回0

试试这个方法可能对你有帮助

/**
 * @param contentURI
 * @return Return the selected ImagePath
 */
private String getRealPathFromURI(Uri contentURI) {
    String result;
    Cursor cursor = yourContext.getContentResolver().query(contentURI,
            null, null, null, null);
    if (cursor == null) {
        result = contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor
                .getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        result = cursor.getString(idx);
        cursor.close();
    }
    return result;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM