【问题标题】:Open an image saved in SD card with option to crop it as in Gallery to apply as wallpaper打开保存在 SD 卡中的图像,可选择将其裁剪为图库中以应用为墙纸
【发布时间】:2013-01-18 15:27:29
【问题描述】:

我有一个应用程序,其中我在 SD 卡 的文件夹中下载了一些图像。我想把它保存为墙纸。

使用下面的代码用户可以将其设置为墙纸。

WallpaperManager myWallpaperManager = WallpaperManager.getInstance(context);
myWallpaperManager.setBitmap(loadedImage);

但是,当从 Gallery 应用程序中选择图像以设置壁纸时,这不会显示任何 UI 供用户选择图像的一部分,例如裁剪操作。我希望我的代码触发这样的操作。当用户单击我的应用程序中的按钮时,我想将他们带到带有裁剪选项的图库应用程序以设置壁纸。

请告诉我该怎么做。谢谢。

【问题讨论】:

  • 您的目标是哪个 Android 版本?
  • 嗨@Shinigamae:我想从 2.1 开始支持。但如果只有特定版本才有可能,我可以接受。
  • 我确实有一个简单的项目,在 2.3 上。我允许用户从他的库中选择一个图像(之前从网站保存)然后允许他裁剪该图像。但我发现它在 Android 3.0 和 4.0 上运行不佳。那么需要一些解决方法。

标签: android android-intent android-camera-intent


【解决方案1】:

你可能想试试这个:

  1. 从您的图库中选择(包括 SD 卡) - void selectPhoto()

    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Choose photo to upload"), PICK_FROM_FILE);
    
  2. 开始裁剪操作 - void doCrop()

    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setType("image/*");
    
    // Check if there is image cropper application installed.
    List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
    
    int size = list.size();
    
    // If no cropper application is found, throw a message.
    if (size == 0) {            
        Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
        return;
    
    // If there're cropper applications found, use the first
    } else {
    
        // Specify image path and cropping parameters
        intent.setData(mImageCaptureUri);
        intent.putExtra("outputX", 0);
        intent.putExtra("outputY", 0);
        intent.putExtra("return-data", true);
    
        Intent i = new Intent(intent);
        ResolveInfo res = list.get(0);
        i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        startActivityForResult(i, CROPPED_IMAGE);
    
  3. 处理活动结果 - void onActivityResult(int requestCode, int resultCode, Intent data)

    if (resultCode != RESULT_OK) return;
    
    switch (requestCode) {
        case PICK_FROM_FILE: 
            mImageCaptureUri = data.getData();
            doCrop();
            break;          
        case CROPPED_IMAGE:         
            Bundle extras = data.getExtras();
            try{
                if (extras != null) {
                     Bitmap myImage = extras.getParcelable("data");
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            break;
    

此代码将在您选择图像后立即激活裁剪操作。

请注意,mImageCaptureUri 是选定的图像 URI,它将传递给裁剪操作的意图。

【讨论】:

  • 感谢您的代码。稍作调整,效果就完美了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-26
  • 1970-01-01
  • 1970-01-01
  • 2011-01-31
  • 1970-01-01
  • 2018-01-17
  • 1970-01-01
相关资源
最近更新 更多