我想知道是否可以创建一个意图,使画廊裁剪器显示突出墙纸的功能。这个功能已经在Honeycomb中引入。要了解我正在寻找什么,请查看图片上的平板电脑(三个蓝色矩形)。
我查看了ICS画廊应用程序源代码,但是我找不到我要找的内容。
我想知道是否可以创建一个意图,使画廊裁剪器显示突出墙纸的功能。这个功能已经在Honeycomb中引入。要了解我正在寻找什么,请查看图片上的平板电脑(三个蓝色矩形)。
我查看了ICS画廊应用程序源代码,但是我找不到我要找的内容。
I/ActivityManager( 94): Starting: Intent {
act=android.intent.action.CHOOSER
cmp=android/com.android.internal.app.ChooserActivity (has extras) } from pid 558
I/ActivityManager( 94): Starting: Intent {
act=android.intent.action.ATTACH_DATA
dat=content://media/external/images/media/648
typ=image/jpeg
flg=0x3000001
cmp=com.google.android.gallery3d/com.cooliris.media.Photographs (has extras) } from pid 558
I/ActivityManager( 94): Starting: Intent {
dat=content://media/external/images/media/648
cmp=com.google.android.gallery3d/com.cooliris.media.CropImage (has extras) } from pid 558
ActivityChooser
pops up and you select "Wallpaper".This selection fires an Intent
with action ATTACH_DATA
and component com.cooliris.media.Photographs
, which is a class in the Android framework that serves as a "wallpaper picker" for the camera application; it just redirects to the standard pick action. Since we have given the Intent
a URI that specifies the image to set as the wallpaper, this class will inevitably execute the following code (see the class's onResume
method):
Intent intent = new Intent();
intent.setClass(this, CropImage.class);
intent.setData(imageToUse);
formatIntent(intent);
startActivityForResult(intent, CROP_DONE);
This fires another Intent
that starts the CropImage
Activity... this is where you specify the cropped area using the soft-rectangle. When you specify the crop, the result is set to RESULT_OK
with requestCode = CROP_DONE
. The Photographs
Activity switch-cases over these two constants and then sets the wallpaper accordingly (see the Photographs
class's onActivityResult
method).
Intent
来执行这些确切的事件序列,您需要在com.cooliris.media
包中查找,并将相关类复制粘贴到您的项目中。根据我的过去经验,这样做往往比它值得的麻烦多(除非它是为了执行相对简单的操作),但绝对是可能的。com.cooliris.media
包。这些类在一起应该足以正确地启动CropImage
活动,但您必须手动设置壁纸以响应CropImage
活动的结果。CropImage
活动,您需要调用:Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
onActivityResult
中,如果requestCode == ACTIVITY_SELECT_IMAGE
并且resultCode == RESULT_OK
,请使用onActivityResult
的第三个参数中给定的Uri数据启动CropImage
Activity(有关如何启动Activity的示例,请参见示例代码)。I this will help:
public class CropSelectedImageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
final Bundle extras = data.getExtras();
Uri photoUri = data.getData();
if (photoUri != null) {
Intent intent = new Intent("com.android.camera.action.CROP");
//intent.setClassName("com.android.camera", "com.android.camera.CropImage");
intent.setData(photoUri);
intent.putExtra("outputX", 96);
intent.putExtra("outputY", 96);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
startActivityForResult(intent, 1);
}
}
}
}
我没有尝试过这个,但如果你看一下这里。
Bundle newExtras = new Bundle();
// maybe that here - for more options see your source code link
newExtras.putString("circleCrop", "true");
Intent cropIntent = new Intent();
// Uri would be something from MediaStore.Images.Media.EXTERNAL_CONTENT_URI
cropIntent.setData(img.fullSizeImageUri());
// edit: it's inside com.android.gallery in case that is even installed.
// should work if you replace that with setClassName("com.android.gallery", "com.android.camera.CropImage")
cropIntent.setClass(this, CropImage.class);
cropIntent.putExtras(newExtras);
startActivityForResult(cropIntent, CROP_MSG);
如果你想尝试一下,这可能会对你有所帮助。
也许可以通过选择意图的方式实现:
Intent i = new Intent(Intent.ACTION_PICK);
i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivity(i);
cropIntent.setClassName("com.android.camera", "com.android.camera.CropImage");
时,我遇到了ActivityNotFoundException异常。此外,我正在寻找一个普通图像选择意图(Intent.ACTION_PICK)的适应,尽管我现在意识到问题在这方面并不清楚。 - ThomascropIntent.setClassName("com.android.gallery", "com.android.camera.CropImage")
。对于Intent.ACTION_PICK
,请参见更新的答案。未经测试,但可能有效。 - zapl有一个很好的库,基于ICS的裁剪屏幕(来自相册应用程序),在这里。
您可以根据需要修改它,以选择要裁剪的部分。
该代码基于Android的Gallery应用程序(链接在这里),位于“/com/android/camera/gallery”,而最重要的类是“/com/android/camera/”中的“CropImage”。即使缺少库,它也适用于所有人(Google的代码始终可用):
git clone https://android.googlesource.com/platform/packages/apps/Gallery3D/
即使这不可用,我相信还会有其他的解决方案。
与其他解决方案相比的优点:
再次强调,裁剪图像最推荐的方法仍然是使用第三方库。不要使用意图的解决方法。
只要这样做!
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA).setDataAndType(contentUri, "image/jpeg")
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
.putExtra("mimeType", "image/jpeg");
startActivity(Intent.createChooser(intent, getString(R.string.set_as)));
试试这个
// Set Home wallpaper the image
public void setHomeWallpaper () {
BitmapDrawable bitmapDrawable = ((BitmapDrawable) imageView.getDrawable());
Bitmap bitmap = bitmapDrawable.getBitmap();
String bitmapPath = Images.Media.insertImage(getContentResolver(), bitmap, "", null);
Uri bitmapUri = Uri.parse(bitmapPath);
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA).setDataAndType(bitmapUri, "image/jpeg")
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
.putExtra("mimeType", "image/jpeg");
startActivity(Intent.createChooser(intent, getString(R.string.background)));
Toast.makeText(PicassoDisplayImageAdapter.this, "قم بإختيار التطبيق المناسب لتعيين الصورة", Toast.LENGTH_SHORT).show();
}