以编程方式使用Android内置图库应用中的图像

时间:2015-04-26 15:40:40

标签: android libgdx

我看了一下这篇文章:Get/pick an image from Android's built-in Gallery app programmatically

我想我明白发生了什么,但从我所看到的他永远不会“使用”所选图像?我需要在某个屏幕上绘制所选图像。

到目前为止我已经这样做了:

public void onActivityResult(int request, int response, Intent data) {
    super.onActivityResult(request, response, data);

    if(response == RESULT_OK) {
        if(request == SELECT_PICTURE) {
            Uri selectedImage = data.getData();
            selectedImagePath = getPath(selectedImage);
            imageView.setImageURI(selectedImage);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            bitImage1 = BitmapFactory.decodeFile(selectedImagePath, options);
            imageView.setImageBitmap(bitImage1);
            imageBoolean = true;

        }
    }
}
public String getPath(Uri uri) {
    // just some safety built in
    if( uri == null ) {
        // TODO perform some logging or show user feedback
        return null;
    }
    // try to retrieve the image from the media store first
    // this will only work for images selected from gallery
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
    if( cursor != null ){
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

    // this is our fallback here
    return uri.getPath();
}
@Override
 public void draw() {
    Canvas canvas = new Canvas();
    canvas.drawBitmap(bitImage1, 0.0f, 0.0f, null);
}
@Override
public void imageUpload() {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {

            intent.setType("image/");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);

        }
    });

}

我在我的屏幕类中的一个按钮上调用了该方法imageUpload(),这会调出图库,然后按下图像并出现此错误:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/6865 (has extras) }} to activity {com.package.android/package.android.AndroidLauncher}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageURI(android.net.Uri)' on a null object reference

这是我需要绘制图像的屏幕类:

public class EditScreen implements Screen, purchaseInterface {
public EditScreen(final Stor gam) {
public void render(float delta) {
....
....
}
public void show() {
boxImage1.addListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            imageUpload();


        }
    });
    if(returnImageSet()) {
        draw();
    }
@Override
public void imageUpload() {
    pInt.imageUpload();
}

编辑:

public String getPath(Uri uri) {
    // just some safety built in
    if( uri == null ) {
        // TODO perform some logging or show user feedback
        return null;
    }
    // try to retrieve the image from the media store first
    // this will only work for images selected from gallery
    String[] projection = { MediaStore.Images.Media.DATA };
    try {
        InputStream inputStream = getContentResolver().openInputStream(uri);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    // this is our fallback here
    return uri.getPath();
}

1 个答案:

答案 0 :(得分:1)

您的getPath()实施适用于少数几款Android设备,每天都会减少。 A Uri is not a file

使用ContentResolveropenInputStream()InputStream中的Uri获取onActivityResult()。然后,使用decodeStream()上的BitmapFactory获取Bitmap。在后台主题上完成所有这些。

或者,使用a third-party image loading library(例如,Picasso,Universal Image Loader)可以为您完成这项工作。