Android-内置图库中不显示图像
我正在尝试使用Android的内置图库。我可以获得图库和相册,但每当我想显示图像时,图库就会直接引导我回到我的应用程序。我无法查看图像,尽管它已被调用 这是我的代码:Android-内置图库中不显示图像,android,image,android-intent,gallery,Android,Image,Android Intent,Gallery,我正在尝试使用Android的内置图库。我可以获得图库和相册,但每当我想显示图像时,图库就会直接引导我回到我的应用程序。我无法查看图像,尽管它已被调用 这是我的代码: public class CameraTab extends Activity implements OnClickListener{ private static final int SELECT_PICTURE = 1; private String selectedImagePath; public void onCrea
public class CameraTab extends Activity implements OnClickListener{
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_tab);
ImageButton cameraBtn = (ImageButton)findViewById(R.id.camera_btn);
cameraBtn.setOnClickListener(this);
ImageButton galleryBtn = (ImageButton)findViewById(R.id.gallery_btn);
galleryBtn.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == this.findViewById(R.id.camera_btn)){
/// some codes here
}
if (v == this.findViewById(R.id.gallery_btn)){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
有人能帮我吗?任何帮助都将不胜感激!!谢谢 问题在于对
意图的误解。行动获取内容意图。它用于从存档中选择内容(在本例中为image/*)
如果要显示图像,只需在布局中创建一个带有ImageView的新活动。使用setData
传递图像URI我认为您要使用的操作是Intent.Action\u VIEW
。
试试这个
final Intent Intent=新意图(Intent.ACTION\u视图);
最终Uri=;
//Uri来自文件,例如。
最终Uri=Uri.fromFile(yourImageFileOnSDCard);
//或者像getPath()方法一样从媒体存储区访问,但使用URI
//从http://developer.android.com/reference/android/provider/MediaStore.Images.Media.html#EXTERNAL_CONTENT_URI
intent.setDataAndType(uri,“image/*”);
星触觉(意向);
字符串“image/*
是要使用的mime类型
现在,已打开图库并选择给定图片。要返回应用程序,用户必须按“上一步”按钮,与往常一样;)
我使用了下面的缩放位图,因为在某些设备上,库中的图像可能太大,无法显示在ImageView上(我以前遇到过这个问题)
final Intent intent = new Intent(Intent.ACTION_VIEW);
final Uri uri = <The URI to your file>;
// Uri either from file eg.
final Uri uri = Uri.fromFile(yourImageFileOnSDCard);
// or from media store like your method getPath() does but with the URI
// from http://developer.android.com/reference/android/provider/MediaStore.Images.Media.html#EXTERNAL_CONTENT_URI
intent.setDataAndType(uri, "image/*");
startActivity(intent);
private Context context;
public void onCreate(Bundle savedInstanceState) {
...
context = this;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
Bitmap b = new BitmapDrawable(context.getResources(),
selectedImagePath).getBitmap();
int i = (int) (b.getHeight() * (512.0 / b.getWidth()));
bitmap = Bitmap.createScaledBitmap(b, 512, i, true);
// To display the image, you need to set it to an ImageView here
ImageView img = (ImageView) findViewById(R.id.myImageView);
img.setImageBitmap(bitmap);
}
}
}