Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 裁剪图像,然后将其保存在库中_Android_Image_Image Processing_Android Gallery - Fatal编程技术网

Android 裁剪图像,然后将其保存在库中

Android 裁剪图像,然后将其保存在库中,android,image,image-processing,android-gallery,Android,Image,Image Processing,Android Gallery,我使用以下代码使用android裁剪意图裁剪图像 Intent cropIntent = new Intent("com.android.camera.action.CROP", null) .setDataAndType(picUri,"image/*") .putExtra("crop", "true") .putExtra("aspectX", 1) .putExtra("aspectY",

我使用以下代码使用android
裁剪意图
裁剪图像

             Intent cropIntent = new Intent("com.android.camera.action.CROP", null)
         .setDataAndType(picUri,"image/*")
         .putExtra("crop", "true")
         .putExtra("aspectX", 1)
         .putExtra("aspectY", 1)
         .putExtra("outputX", 128)
         .putExtra("outputY", 128)
         .putExtra("scale", true)
         .putExtra("return-data", false)
         .putExtra("scaleUpIfNeeded", true)
         .putExtra(MediaStore.EXTRA_OUTPUT, picUri)
         .putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
         startActivityForResult(cropIntent, PIC_CROP);
然后,我从这段代码中得到了裁剪过的图片,我想将其保存在gallery中一个单独的文件夹(由我的应用程序创建)中

以下是保存图像的代码:

    public static String SaveImage(Bitmap finalBitmap)
{
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/Shopic Snaps");

    if(!myDir.exists())
        myDir.mkdirs();

    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image_"+ n+ GenerateRandomName() +".jpg";
    File file = new File (myDir, fname);

    if (file.exists ()) 
        file.delete ();

    try
    {
           FileOutputStream out = new FileOutputStream(file);
           finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();
    }
    catch (Exception e)
    {
           e.printStackTrace();
    }

    return root + "/App Snaps/"+fname;
}
现在这个函数应该将图片保存在gallery中,并返回图片的路径。代码正常运行,没有错误

但在执行完任务后,当我检查库时,它是空的。我不知道为什么不把照片保存在画廊里

编辑:

    public static String SaveImage(Bitmap finalBitmap)
{
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/Shopic Snaps");

    if(!myDir.exists())
        myDir.mkdirs();

    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image_"+ n+ GenerateRandomName() +".jpg";
    File file = new File (myDir, fname);

    if (file.exists ()) 
        file.delete ();

    try
    {
           FileOutputStream out = new FileOutputStream(file);
           finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();
    }
    catch (Exception e)
    {
           e.printStackTrace();
    }

    return root + "/App Snaps/"+fname;
}
我在另一部手机上测试了这个应用程序,照片保存得很好,正在显示中。所以我想这是我手机的一个问题,但我不知道是什么导致了这个问题。

//初始化意图
// Initialize intent
Intent intent = new Intent("com.android.camera.action.CROP");
// set data type to be sent
intent.setType("image/*");

// get croppers available in the phone
List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
int size = list.size();
// handle the case if there's no cropper in the phone
if (size == 0) {
    Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
    return;
} else {

// now this is the case if cropper exists
// initialize the Uri for the captures or gallery image

    Uri imageUri = Uri.fromFile(new File(mCurrentPhotoPath));

    // Send the Uri path to the cropper intent
    intent.setData(imageUri); 
    intent.putExtra("outputX", 200);
    intent.putExtra("outputY", 200);
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);         
    intent.putExtra("scale", true);
    // Here's my attempt to ask the intent to save output data as file
    File f = null;
    // Here I initialize empty file 
    try{
            // This returns the file created
        f = setUpCroppedFile();
        mCurrentThumbPath = f.getAbsolutePath();            
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));            
        intent.putExtra("output", Uri.fromFile(f));         
    }
    catch(IOException e){
        e.printStackTrace();
    }

    intent.putExtra("return-data", false);
    // --------------------------------------------------------------------
    // --------------------------------------------------------------------

    // If there's only 1 Cropper in the phone (e.g. Gallery )
    if (size == 1) {
            // get the cropper intent found
            Intent i        = new Intent(intent);
            ResolveInfo res = list.get(0);

            i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));

            startActivityForResult(i, CROPPED_PHOTO);
    }
Intent Intent=newintent(“com.android.camera.action.CROP”); //设置要发送的数据类型 intent.setType(“image/*”); //在手机中获取可用的裁剪器 List List=getPackageManager().queryInputActivities(intent,0); int size=list.size(); //如果电话里没有窃听器,就处理这个案子 如果(大小==0){ Toast.makeText(这是“找不到图像裁剪应用程序”,Toast.LENGTH_SHORT.show(); 返回; }否则{ //现在,如果存在裁剪器,情况就是这样 //初始化捕获或库图像的Uri uriimageuri=Uri.fromFile(新文件(mcurrentprotopath)); //将Uri路径发送到裁剪器 intent.setData(imageUri); 意向。额外(“输出”,200); 意向。额外投入(“产出”,200); intent.putExtra(“aspectX”,1); 意图.putExtra(“aspectY”,1); 意图。putExtra(“比例”,真实); //下面是我试图询问将输出数据保存为文件的意图 文件f=null; //这里我初始化空文件 试一试{ //这将返回创建的文件 f=设置裁剪文件(); mCurrentThumbPath=f.getAbsolutePath(); intent.putExtra(MediaStore.EXTRA_输出,Uri.fromFile(f)); intent.putExtra(“输出”,Uri.fromFile(f)); } 捕获(IOE异常){ e、 printStackTrace(); } intent.putExtra(“返回数据”,false); // -------------------------------------------------------------------- // -------------------------------------------------------------------- //如果手机中只有一个裁剪器(例如Gallery) 如果(大小==1){ //找到裁剪者的意图 意向i=新意向(意向); ResolveInfo res=list.get(0); i、 setComponent(新组件名(res.activityInfo.packageName,res.activityInfo.name)); startActivityForResult(一,截图); }
检查文件资源管理器,文件是否存储在设备中。如果文件已存储但未显示在库中。请尝试此操作 注意:按照内部和外部SD卡的步骤操作

转到文件管理器->安卓->数据->com.Android.gallery3d。 删除内部和外部SD卡中的文件夹(com.android.gallery3d)。 进入设置->应用程序/应用程序管理器->搜索库->打开库并点击清除数据。 关闭手机并等待几分钟(比如2-3分钟),然后打开并等待几分钟。
现在就是这样。所有的图像和视频都将显示在Gallery下。

文件是否存储在所需的目录中,并且是否有效?如果是,read file不会存储在那里,但有时会存储在那里,我不知道它何时存储,何时不存储。您知道,在代码中,返回的目录与您存储的目录不同e文件(“Shopic快照”“应用程序快照”)?Android没有
裁剪
意图
:抱歉,回复太晚,哦,实际上我意外更改了路径。抱歉,顺便说一句,代码中没有问题,图片没有显示在多媒体资料中。问题是,虽然代码可以正常工作,但它没有将图片保存在手机多媒体资料中。SSO而不是po在打印我的答案时,您刚刚复制并粘贴了答案!!什么是setUpCroppedFile()?