以编程方式使用 Android 的内置图库应用中的图像
USE an image from Android's built-in Gallery app programmatically
我看了这个post:Get/pick an image from Android's built-in Gallery app programmatically
我想我明白发生了什么,但据我所知,他从来没有 "uses" 选择的图像?我需要将选定的图像绘制在某个屏幕上。
到目前为止我已经这样做了:
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);
}
});
}
我在屏幕上的按钮 class 上调用了该方法 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
这是我的屏幕class,我需要在其中绘制图像:
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();
}
您的 getPath()
实施将在少数 Android 设备上运行,并且每天都在减少。 A Uri
is not a file.
使用 ContentResolver
和 openInputStream()
为 onActivityResult()
中给出的 Uri
获得 InputStream
。然后,在 BitmapFactory
上使用 decodeStream()
得到一个 Bitmap
。在后台线程上执行所有这些操作。
或者,使用可以为您完成这项工作的 a third-party image loading library(例如,Picasso、Universal Image Loader)。
我看了这个post:Get/pick an image from Android's built-in Gallery app programmatically
我想我明白发生了什么,但据我所知,他从来没有 "uses" 选择的图像?我需要将选定的图像绘制在某个屏幕上。
到目前为止我已经这样做了:
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);
}
});
}
我在屏幕上的按钮 class 上调用了该方法 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
这是我的屏幕class,我需要在其中绘制图像:
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();
}
您的 getPath()
实施将在少数 Android 设备上运行,并且每天都在减少。 A Uri
is not a file.
使用 ContentResolver
和 openInputStream()
为 onActivityResult()
中给出的 Uri
获得 InputStream
。然后,在 BitmapFactory
上使用 decodeStream()
得到一个 Bitmap
。在后台线程上执行所有这些操作。
或者,使用可以为您完成这项工作的 a third-party image loading library(例如,Picasso、Universal Image Loader)。