gpt4 book ai didi

android - 从图库中选取时裁剪图像,但从相机中单击时不裁剪图像

转载 作者:行者123 更新时间:2023-11-29 00:05:46 25 4
gpt4 key购买 nike

我知道这个问题是 SO 社区上其他问题的副本,但我正在重新询问它,因为我有一些不同的代码并且无法找到解决我的问题的方法。当我从图库中获取图像时,我的代码正在工作(意味着要裁剪 Intent ),但是当我尝试单击图像时,它从不裁剪图像并尝试运行应在裁剪后运行的代码。这是我的代码:

@Override
public boolean onContextItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.take_photo:
//Toast.makeText(context, "Selected Take Photo", Toast.LENGTH_SHORT).show();
takePhoto();
break;

case R.id.choose_gallery:
//Toast.makeText(context, "Selected Gallery", Toast.LENGTH_SHORT).show();
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
break;

case R.id.share_cancel:
closeContextMenu();
break;
default:
return super.onContextItemSelected(item);
}
return true;
}
public void takePhoto()
{
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra("crop", "true");
File folder = new File(Environment.getExternalStorageDirectory() + "/LoadImg");
if(!folder.exists()) {
folder.mkdir();
}
final Calendar c = Calendar.getInstance();
String new_Date= c.get(Calendar.DAY_OF_MONTH)+"-"+((c.get(Calendar.MONTH))+1) +"-"+c.get(Calendar.YEAR) +" " + c.get(Calendar.HOUR) + "-" + c.get(Calendar.MINUTE)+ "-"+ c.get(Calendar.SECOND);
path=String.format(Environment.getExternalStorageDirectory() +"/LoadImg/%s.png","LoadImg("+new_Date+")");
File photo = new File(path);
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photo));
startActivityForResult(intent, 2);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1) {
photoUri=data.getData();
if (photoUri != null) {
crop(photoUri);
// new GetImages().execute();
}
}

if(requestCode==2) {
Log.v("Load Image", "Camera File Path=====>>>"+path);
image_list.add(path);
Log.v("Load Image", "Image List Size=====>>>"+image_list.size());
//updateImageTable();
//new GetImages().execute();
crop(photoUri);
}
if (requestCode == 3) {
Bundle extras = data.getExtras();
String selectedImagePath = mCropImagedUri.getPath();

Log.i("TAG", "After Crop selectedImagePath " + selectedImagePath);

if (extras != null) {

Log.i("TAG", "Inside Extra " + selectedImagePath);
Bitmap photo = (Bitmap) extras.get("data");


Log.i("TAG", "new selectedImagePath before file "
+ selectedImagePath);
Log.i("TAG", "After File Created " + selectedImagePath);
image_list.add(selectedImagePath);
}
new GetImages().execute();
}
}

请指出正确的方向,因为我是 android 开发的菜鸟。谢谢

编辑在 @johnrao07 建议的回答后,我收到以下错误:

11-24 17:16:35.828: E/AndroidRuntime(8399): FATAL EXCEPTION: main
11-24 17:16:35.828: E/AndroidRuntime(8399): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {com.example.camera/com.example.camera.MainActivity}: java.lang.NullPointerException
11-24 17:16:35.828: E/AndroidRuntime(8399): at android.app.ActivityThread.deliverResults(ActivityThread.java:3141)
11-24 17:16:35.828: E/AndroidRuntime(8399): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
11-24 17:16:35.828: E/AndroidRuntime(8399): at android.app.ActivityThread.access$1100(ActivityThread.java:130)
11-24 17:16:35.828: E/AndroidRuntime(8399): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
11-24 17:16:35.828: E/AndroidRuntime(8399): at android.os.Handler.dispatchMessage(Handler.java:99)
11-24 17:16:35.828: E/AndroidRuntime(8399): at android.os.Looper.loop(Looper.java:137)
11-24 17:16:35.828: E/AndroidRuntime(8399): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-24 17:16:35.828: E/AndroidRuntime(8399): at java.lang.reflect.Method.invokeNative(Native Method)
11-24 17:16:35.828: E/AndroidRuntime(8399): at java.lang.reflect.Method.invoke(Method.java:511)
11-24 17:16:35.828: E/AndroidRuntime(8399): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-24 17:16:35.828: E/AndroidRuntime(8399): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-24 17:16:35.828: E/AndroidRuntime(8399): at dalvik.system.NativeStart.main(Native Method)
11-24 17:16:35.828: E/AndroidRuntime(8399): Caused by: java.lang.NullPointerException
11-24 17:16:35.828: E/AndroidRuntime(8399): at com.example.camera.MainActivity.onActivityResult(MainActivity.java:263)
11-24 17:16:35.828: E/AndroidRuntime(8399): at android.app.Activity.dispatchActivityResult(Activity.java:5192)
11-24 17:16:35.828: E/AndroidRuntime(8399): at android.app.ActivityThread.deliverResults(ActivityThread.java:3137)
11-24 17:16:35.828: E/AndroidRuntime(8399): ... 11 more

最佳答案

if(requestCode==2) {
Log.v("Load Image", "Camera File Path=====>>>"+path);
image_list.add(path);
Log.v("Load Image", "Image List Size=====>>>"+image_list.size());
//updateImageTable();
//new GetImages().execute();
//crop(photoUri); **// THE VALUE OF photoUri is null here.**
photoUri = Uri.parse(path);
crop(photoUri); // THIS MAY BE WORK FOR YOU.
}

关于android - 从图库中选取时裁剪图像,但从相机中单击时不裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33893221/

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