gpt4 book ai didi

android - 通过 android gallery 裁剪方法裁剪可绘制图像

转载 作者:行者123 更新时间:2023-11-29 01:59:02 25 4
gpt4 key购买 nike

单击我的 Imageview 时,我想通过 android gallery 的默认裁剪技术裁剪位于我的应用程序的可绘制文件夹中的 imageview 的背景图像,裁剪后的图像应在裁剪后设置为相同。

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(Uri.parse("android.resource://com.example.croppingactivity/drawable/apple"), "image/*");
intent.putExtra("outputX", 400);
intent.putExtra("outputY", 400);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("crop", true);
//intent.putExtra("output", Uri.parse("android.resource://com.example.croppingactivity/drawable/apple"));
startActivityForResult(intent, 1);
}

这是我的堆栈跟踪。

12-04 10:21:28.812: E/AndroidRuntime(2553): FATAL EXCEPTION: main
12-04 10:21:28.812: E/AndroidRuntime(2553): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.GET_CONTENT dat=android.resource://com.example.croppingactivity2130837504 typ=image/* (has extras) }
12-04 10:21:28.812: E/AndroidRuntime(2553): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)
12-04 10:21:28.812: E/AndroidRuntime(2553): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
12-04 10:21:28.812: E/AndroidRuntime(2553): at android.app.Activity.startActivityForResult(Activity.java:3370)
12-04 10:21:28.812: E/AndroidRuntime(2553): at android.app.Activity.startActivityForResult(Activity.java:3331)
12-04 10:21:28.812: E/AndroidRuntime(2553): at com.example.croppingactivity.MainActivity$1.onClick(MainActivity.java:52)
12-04 10:21:28.812: E/AndroidRuntime(2553): at android.view.View.performClick(View.java:4202)
12-04 10:21:28.812: E/AndroidRuntime(2553): at android.view.View$PerformClick.run(View.java:17340)
12-04 10:21:28.812: E/AndroidRuntime(2553): at android.os.Handler.handleCallback(Handler.java:725)
12-04 10:21:28.812: E/AndroidRuntime(2553): at android.os.Handler.dispatchMessage(Handler.java:92)
12-04 10:21:28.812: E/AndroidRuntime(2553): at android.os.Looper.loop(Looper.java:137)
12-04 10:21:28.812: E/AndroidRuntime(2553): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-04 10:21:28.812: E/AndroidRuntime(2553): at java.lang.reflect.Method.invokeNative(Native Method)
12-04 10:21:28.812: E/AndroidRuntime(2553): at java.lang.reflect.Method.invoke(Method.java:511)
12-04 10:21:28.812: E/AndroidRuntime(2553): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-04 10:21:28.812: E/AndroidRuntime(2553): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-04 10:21:28.812: E/AndroidRuntime(2553): at dalvik.system.NativeStart.main(Native Method)

但它不起作用。我收到 Activity 未找到异常。我做错了什么请帮助我。

最佳答案

好吧,这是我用来做这种事情的代码。也许你可以试试:

final Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");

intent.putExtra("crop", "true");

intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);

intent.putExtra("outputX", 400);
intent.putExtra("outputY", 400);
// Scale the image down to 400 x 400
intent.putExtra("scale", true);

intent.putExtra("windowTitle", "My_title");
// You need to use a temporary file for cropping to work
File tempFile = File.createTempFile("crop", "png",
myActivity.getCacheDir());
mSavedUri = Uri.fromFile(tempFile);
intent.putExtra("output", mSavedUri);
intent.putExtra("outputFormat", "PNG");

profileActivity.startActivityForResult(intent, INTENT_PICK_PICTURE);

我猜你必须将 intent.setType(...) 更改为 intent.setDataAndType(Uri.parse("android.resource://...") , "图片/*")

关于android - 通过 android gallery 裁剪方法裁剪可绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13639773/

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