gpt4 book ai didi

android - 选择图片后 Activity 力销毁

转载 作者:行者123 更新时间:2023-11-30 02:05:14 25 4
gpt4 key购买 nike

我有一个简单的选择图像源对话框:

public void toggleImagePicker()
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Choose Image Source");
builder.setItems(new CharSequence[]{"Gallery", "Camera"}, new DialogInterface.OnClickListener()
{

@Override
public void onClick(DialogInterface dialog, int which)
{
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
// inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
switch (which) {
case 0:
// GET IMAGE FROM THE GALLERY
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");

Intent chooser = Intent.createChooser(intent, "Choose a Picture");
if (mBackgroundClicked)
startActivityForResult(chooser, REQUEST_GALLERY_IMAGE_FOR_BACKGROUND);
else
startActivityForResult(chooser, REQUEST_GALLERY_IMAGE);
break;

case 1:
Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");

File cameraFolder;

if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(), ".helper/");
else
cameraFolder = getActivity().getCacheDir();
if (!cameraFolder.exists())
cameraFolder.mkdirs();

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
String timeStamp = dateFormat.format(new Date());
String imageFileName = "picture_" + timeStamp + ".jpg";

File photo = new File(Environment.getExternalStorageDirectory(), ".helper/" + imageFileName);
if (!mBackgroundClicked)
mProfilePhotoPath = new String(photo.getAbsolutePath());
else
mBackgroundPhotoPath = new String(photo.getAbsolutePath());
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
// initialURI = Uri.fromFile(photo);
if (mBackgroundClicked)
startActivityForResult(getCameraImage, REQUEST_IMAGE_CAPTURE_FOR_BACKGROUND);
else
startActivityForResult(getCameraImage, REQUEST_IMAGE_CAPTURE);

break;

default:
break;
}
}
});

builder.show();
}

然后我尝试选择图像,显示对话框并单击“相机”或“画廊”。否则我的 Activity 将被破坏并重新创建。

No exceptions in LogCat.

我在日志中发现的一件事:

06-11 14:45:57.700      458-549/? W/InputDispatcher﹕ channel '221e47c0 ******.ProfileExpertActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9

注意:选择机制在 fragment 中实现。

最佳答案

如果没有应用程序来处理该操作,那么它将在开始使用 resolveActivity 检查之前导致 NullPointerException检查是否有任何应用程序处理它。

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
// Verify that the intent will resolve to an activity
if (photoPickerIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(photoPickerIntent, GALLERY_PICKER);
}

关于android - 选择图片后 Activity 力销毁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30779968/

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