- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我遇到了一个看起来很简单的简单问题,但是,当我从图库中挑选一张图片并尝试在 onActivityResult 的 ImageView 中设置它时,就会出现错误。
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/35 }} to activity {com.mypackagename/com.mypackagename.SQLiteActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
这个错误一定是由于空对象。这是否意味着没有检索到任何数据?我认为我所做的设置没有问题,但是,我可能遗漏了一些东西。我在下面设置了示例代码。
这是调用图库的代码
public void settingImage(View v){
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
这段代码是用来获取结果的
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getApplicationContext().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
// Log.d("Path", picturePath);
//sampleimage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
// imgPath = picturePath;
icontext.setText(picturePath);
cursor.close();
} else {
}
}
这里是一些设置
private static int RESULT_LOAD_IMAGE = 1;
基本的SDK设置
minSdkVersion 14
targetSdkVersion 22
最佳答案
我试过这个来获取位图,
调用图库选择器的代码,
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
获取位图结果,
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PHOTO:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
//yourSelectedImage = BitmapFactory.decodeStream(imageStream);
try {
yourSelectedImage = decodeUri(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// imgViewProfilePic.setImageBitmap(yourSelectedImage);
}
}
}
解码uri,
private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 140;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);
}
关于android - 无法使用 Intent.ACTION_PICK 从图库中获取位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30274199/
我希望用户选择我的应用程序将向其发送 SMS 的联系人。我如何确保当我使用 ACTION_PICK Intent 开始一个 Activity 时,只显示那些有手机号码的联系人? 目前,我正在这样开始
我正在尝试使用 Android 画廊来挑选图像。为此,启动画廊很容易 Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); photoP
我有一个应用程序,其中一个方面是让用户选择一个联系人并通过该应用程序向该联系人发送文本。该应用程序仅适用于某些联系人,而对其他人则失败。更准确地说: 对于我手动输入通讯录的联系人,Intent.ACT
我的代码允许用户通过单击按钮使用 Intent.ACTION_PICK 选择图像: public void onClick(View v) { Intent photoPickerIntent = n
我正在尝试使用 intent.ACTION_PICK 打开图像,但是当我使用 startActivityForResoult 启动 Activity 时,我的应用程序崩溃了。我做错了什么的任何线索?
我正在尝试制作一个应用程序,要求用户选择一个图像文件,然后通过 Intent 将其发送到另一个应用程序(在这种情况下是 Whatsapp,但这应该适用于其他应用程序) 我这样做是为了请求文件: In
我遇到了一个看起来很简单的简单问题,但是,当我从图库中挑选一张图片并尝试在 onActivityResult 的 ImageView 中设置它时,就会出现错误。 java.lang.RuntimeE
我想从构建联系人选择器中读取联系人信息。我正在读取电话号码、街道、电子邮件和地址。它出错了。 以下是调用联系人选择器的代码: Intent contactPickerIntent = new Inte
我正在使用这样的 Intent : Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore
我想知道以下 android intents 是否在所有设备上都可用,或者它们是否仅在某些设备上可用,如 com.android.camera.action.crop 是? 从图库中选择图像的 Int
代码: Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("audio/*"); startActivityForRe
我在 onCreate Activity 类中有这段代码,但我不想使用 Activity。 Intent intent = new Intent(Intent.ACTION_PICK, android
我需要在我的应用程序中选择联系人,并希望排除存储在我的 SIM 卡中的联系人。 ACTION_PICK 是否可行? 最佳答案 不,这不可能 很遗憾,目前不可能。 为了证明这一点,让我们深入研究Cont
我的目标是只向用户显示带有电话号码的联系人,并让用户选择我想存储在本地的几个联系人。 我在下面的方法中使用了各种选项来代替 ContactsContract.Contacts.CONTENT_URI。
我在 Android 中开发,我想实现一个文本文件选择。 我在我的安卓手机上安装了es文件管理器。 我添加权限: ,然后我尝试以下代码: Intent intent = new Intent( In
我正在编写一个 Android 应用,它的数据类型代表一个人(特别是 child 的 parent 或监护人)。我希望能够从 Android 设备的联系人数据库中“导入”相关数据字段。 (这应该是可选
我试图让用户在我正在构建的这个壁纸应用程序中选择他们想要在他们的设备上用作壁纸的任何图像。出于某种原因,当我写作时: Intent myIntent = new Intent(Intent.ACTIO
我正在开发一个 Android 应用程序,其中包含一个带有图像的 GridView 。我希望此应用程序能够在用户想要选择图像时做出响应。我已将以下内容放入我的应用程序 list 中:
我正在运行 ICS 并使用相同 Google 帐户的平板电脑和手机上测试我们的应用程序。如果我在一个设备上拍照,它会显示在另一台设备上(通过 Picasa 同步)。奇怪的是,当我做正常的时候,照片会出
public class MainActivity extends Activity { Button b; //FrameLayout fl; @Override p
我是一名优秀的程序员,十分优秀!