【问题标题】:Insert picture to gallery, name wrong将图片插入图库,名称错误
【发布时间】:2014-03-12 00:41:25
【问题描述】:

我想将图片保存到图库中。它适用于下面的代码,但图片中的名称是 System.currentTimeMillis .jpg 有没有人建议我可以更改名称就像我的头衔一样?

/**
 * Insert an image and create a thumbnail for it.
 *
 * @param cr The content resolver to use
 * @param source The stream to use for the image
 * @param title The name of the image
 * @param description The description of the image
 * @return The URL to the newly created image, or <code>null</code> if the image failed to be stored
 * for any reason.
 */
private static final String insertImage(ContentResolver cr, Bitmap source,
        String title, String description) {
    ContentValues values = new ContentValues();
    values.put(Media.TITLE, title);
    values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
    values.put(Images.Media.MIME_TYPE, "image/jpeg");

    Uri uri = null;
    String stringUrl = null; /* value to be returned */

    try {
        uri = cr.insert(Images.Media.EXTERNAL_CONTENT_URI, values);

        if (source != null) {
            OutputStream imageOut = cr.openOutputStream(uri);

            try {
                source.compress(Bitmap.CompressFormat.JPEG, 100, imageOut);
            } finally {                 
                imageOut.close();
            }

            long id = ContentUris.parseId(uri);
            // Wait until MINI_KIND thumbnail is generated.
            Bitmap miniThumb = Images.Thumbnails.getThumbnail(cr, id,
                    Images.Thumbnails.MINI_KIND, null);
            // This is for backward compatibility.
            Bitmap microThumb = StoreThumbnail(cr, miniThumb, id, 50F, 50F,
                    Images.Thumbnails.MICRO_KIND);
        } else {
            Log.e(TAG, "Failed to create thumbnail, removing original");
            cr.delete(uri, null, null);
            uri = null;
        }
    } catch (Exception e) {
        Log.e(TAG, "Failed to insert image", e);
        if (uri != null) {
            cr.delete(uri, null, null);
            uri = null;
        }
    }

    if (uri != null) {
        stringUrl = uri.toString();
    }

    return stringUrl;
}

编辑: 我用这段代码调用方法:

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());      
    insertImage(exploreActivity.getContentResolver(), bitmap, companyName+ timeStamp + ".jpg",companyName+ timeStamp + ".jpg");

非常感谢您的任何帮助。

【问题讨论】:

    标签: android image insert gallery


    【解决方案1】:

    您没有在上面代码中的任何地方指定图像名称,这很难调试

    另一种方法是

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");    
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    // Write your image name here
    String fname = "Image-"+ n +".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();
    }
    

    还要确保您在清单文件中具有以下权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    

    立即将图像添加到图库(感谢 Lukas)

     exploreActivity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); 
    

    【讨论】:

    • 谢谢,但是这段代码让我回到了我的老问题:stackoverflow.com/questions/21759476/… 我希望图像完全在文件夹中,就像普通相机应用程序中的照片一样,并且创建日期必须是正确的
    • 据我所知,相机拍摄的默认文件夹是 DCIM。您也可以指定文件夹名称。
    • 答案解决了,没想到现在名字错了
    • 您的答案是正确的,但图片不会立即出现在图库中。如果你添加这个: exploreActivity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); (立即将图像添加到图库)到您的答案,我会将其标记为正确。
    • @lukas 更新了答案。
    【解决方案2】:
     Bitmap originalImage= capturelayout.getDrawingCache(); 
                if (ensureSDCardAccess())       
                {        
                        System.runFinalization();
                        Runtime.getRuntime().gc();
                        System.gc();                                
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
                        curentDateandTime = sdf.format(new Date());                          
                        File file = new File(mScreenshotPath + "/"+curentDateandTime+".png");                               
                        FileOutputStream fos;    
                        try      
                        {                               
                            System.runFinalization();
                            Runtime.getRuntime().gc();
                            System.gc();   
                            fos = new FileOutputStream(file);                                             
                            originalImage.compress(Bitmap.CompressFormat.PNG, 100, fos);                                
                            fos.close();               
                        }     
                        catch (FileNotFoundException e)        
                        {
                           // Log.e("Panel", "FileNotFoundException", e);
                        }        
                        catch (IOException e)                                      
                        {
                           // Log.e("Panel", "IOEception", e);    
                        }                                          
                    }      
    

    【讨论】:

    • mScreenshotPath 的值为私有 String mScreenshotPath = Environment.getExternalStorageDirectory() + "/foldername";
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多