[英]Pick image from Gallery in android app : “Method called after release() ”
[英]How To Pick Image From Gallery in Android App
我正在尝试在Android中制作一个将Image Gallery与我的应用程序集成的应用程序。
以下是我的应用程序执行的任务:
1.第一个屏幕向用户显示和图像视图以及一个加载图片的按钮。
2.单击“加载图片”按钮后,用户将被重定向到Android的图片库,她可以在其中选择一张图片。
3.选择图像后,图像将被加载到主屏幕的图像视图中。
除了上述三个任务,我还需要我的应用执行以下任务:
4.当选择图像时,它显示在主屏幕上,当我选择更多的“加载图片”选项时,先前选择的图像消失了,但是我需要我的应用程序不隐藏先前选择的图像,对所有选定的图像进行实作它显示在主屏幕上的水平滚动列表中。当我从水平滚动列表中选择任何一个时,它们都显示在主屏幕上。
为此,我的xml代码是:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/imgView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"></ImageView>
<Button
android:id="@+id/buttonLoadPicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Load Picture"
android:layout_gravity="center"></Button>
</LinearLayout>
Java代码:
package com.ohile.imagegallerydemo;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class ImageGallery extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_gallery);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
您可以使用ListView(水平滚动)或GridView显示所有选定的图像。 您可以从本教程中了解 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.