gpt4 book ai didi

java - 当我想更改 imageView 时,intent.getData() 为 null

转载 作者:行者123 更新时间:2023-12-01 13:11:56 25 4
gpt4 key购买 nike

我想在点击 imageView 时更改我的头像。

代码:

ivAvatar.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

final CharSequence[] items = {"Prendre une photo",
"Choisir une image", "Annuler"};

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getActivity());

// set title
alertDialogBuilder.setTitle("Avatar :");

// set dialog message
alertDialogBuilder.setCancelable(false).setItems(items,
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int item) {

if (item == 0) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE,
"");
mCapturedImageURI = getActivity()
.getContentResolver()
.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);

Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);

intent.putExtra(MediaStore.EXTRA_OUTPUT,
mCapturedImageURI);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 170);
intent.putExtra("aspectY", 170);
intent.putExtra("outputX", 5000);
intent.putExtra("outputY", 5000);
startActivityForResult(Intent
.createChooser(intent,
"Appareil photo"),
CAPTURE_PICTURE);
}

if (item == 1) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);

intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 170);
intent.putExtra("aspectY", 170);
intent.putExtra("outputX", 5000);
intent.putExtra("outputY", 5000);
startActivityForResult(Intent
.createChooser(intent,
"Choisir une application"),
SELECT_PICTURE);
}

if (item == 2) {
dialog.cancel();
}

}
});

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// afficher
alertDialogBuilder.show();

}
});

public void onActivityResult(int requestCode, int resultCode, Intent data) {

Log.i("TAG", data + " : data");
Log.i("TAG", data.getData() + " : data.getData()");
Log.i("TAG", getActivity().getApplicationContext() + " : context");

if (resultCode == Activity.RESULT_OK) {
if (requestCode == CAPTURE_PICTURE) {

try {
ivAvatar.setImageBitmap(decodeUri(getActivity().getApplicationContext(),
mCapturedImageURI, 1000));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

if (requestCode == SELECT_PICTURE) {

Uri selectedimg = data.getData();
try {
ivAvatar.setImageBitmap(decodeUri(getActivity().getApplicationContext(), selectedimg, 170));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
}

public static Bitmap decodeUri(Context c, Uri uri, final int requiredSize)
throws FileNotFoundException {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri),
null, o);

int widthTmp = o.outWidth, heightTmp = o.outHeight;
int scale = 1;

while (true) {
if (widthTmp / 2 < requiredSize || heightTmp / 2 < requiredSize) {
break;
}
widthTmp /= 2;
heightTmp /= 2;
scale *= 2;
}

BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(c.getContentResolver()
.openInputStream(uri), null, o2);
}

我的错误:

04-01 14:46:54.139: E/AndroidRuntime(11422): FATAL EXCEPTION: main
04-01 14:46:54.139: E/AndroidRuntime(11422): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { (has extras) }} to activity {fr.allbrary/fr.allbrary.activity.ActivityIdentification}: java.lang.NullPointerException
04-01 14:46:54.139: E/AndroidRuntime(11422): at android.app.ActivityThread.deliverResults(ActivityThread.java:3406)
04-01 14:46:54.139: E/AndroidRuntime(11422): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3449)
04-01 14:46:54.139: E/AndroidRuntime(11422): at android.app.ActivityThread.access$1200(ActivityThread.java:150)
04-01 14:46:54.139: E/AndroidRuntime(11422): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
04-01 14:46:54.139: E/AndroidRuntime(11422): at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 14:46:54.139: E/AndroidRuntime(11422): at android.os.Looper.loop(Looper.java:176)
04-01 14:46:54.139: E/AndroidRuntime(11422): at android.app.ActivityThread.main(ActivityThread.java:5279)
04-01 14:46:54.139: E/AndroidRuntime(11422): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 14:46:54.139: E/AndroidRuntime(11422): at java.lang.reflect.Method.invoke(Method.java:511)
04-01 14:46:54.139: E/AndroidRuntime(11422): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
04-01 14:46:54.139: E/AndroidRuntime(11422): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
04-01 14:46:54.139: E/AndroidRuntime(11422): at dalvik.system.NativeStart.main(Native Method)
04-01 14:46:54.139: E/AndroidRuntime(11422): Caused by: java.lang.NullPointerException
04-01 14:46:54.139: E/AndroidRuntime(11422): at android.content.ContentResolver.openInputStream(ContentResolver.java:436)
04-01 14:46:54.139: E/AndroidRuntime(11422): at fr.allbrary.fragment.FragmentInscription.decodeUri(FragmentInscription.java:556)
04-01 14:46:54.139: E/AndroidRuntime(11422): at fr.allbrary.fragment.FragmentInscription.onActivityResult(FragmentInscription.java:530)
04-01 14:46:54.139: E/AndroidRuntime(11422): at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:166)
04-01 14:46:54.139: E/AndroidRuntime(11422): at android.app.Activity.dispatchActivityResult(Activity.java:5456)
04-01 14:46:54.139: E/AndroidRuntime(11422): at android.app.ActivityThread.deliverResults(ActivityThread.java:3402)
04-01 14:46:54.139: E/AndroidRuntime(11422): ... 11 more

为什么data.getData() == null

最佳答案

在 Nexus 4 和 Kitkat 上尝试过。如果我选择标准图库,data.getData() 对我来说也是空的。对于 QuickPic 或其他图库,getData() 返回正确的 uri。

如果 getData() 为 null 试试这个:

     Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");

bundle 中还有另一个条目,其中包含键“cropped-rect”。

关于java - 当我想更改 imageView 时,intent.getData() 为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22786575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com