简体   繁体   English

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

[英]USE an image from Android's built-in Gallery app programmatically

I took a look at this post: Get/pick an image from Android's built-in Gallery app programmatically 我看了一下这篇文章: 以编程方式从Android内置的Gallery应用程序获取/选择图像

I think I understand what´s going on but from what I can see he never "uses" the selected image? 我想我了解发生了什么,但是从我看到的内容中,他从未“使用”选定的图像? I need the selected image to be drawn on a certain Screen. 我需要将选定的图像绘制在某个屏幕上。

I´ve done this so far: 到目前为止,我已经做到了:

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);

        }
    });

}

I call that method imageUpload() on a button in my screen class, which brings up the gallery, then I press an image and get this error: 我在屏幕类中的按钮上调用该方法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

this is my screen class where I need the image to be drawn: 这是我的屏幕课程,需要绘制图像:

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();
}

EDIT: 编辑:

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();
}

Your getPath() implementation will work on few Android devices, getting fewer by the day. 您的getPath()实现将在少数Android设备上运行,并且每天会越来越少。 A Uri is not a file . Uri不是文件

Use a ContentResolver and openInputStream() to get an InputStream for the Uri you are given in onActivityResult() . 使用ContentResolveropenInputStream()获取在onActivityResult()中给定的UriInputStream Then, use decodeStream() on BitmapFactory to get a Bitmap . 然后,在BitmapFactory上使用decodeStream()获得Bitmap Do all of this on a background thread. 在后台线程上执行所有这些操作。

Or, use a third-party image loading library (eg, Picasso, Universal Image Loader) that can do this work for you. 或者,使用可以为您完成此工作的第三方图像加载库 (例如Picasso,Universal Image Loader)。

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

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