Android 将图片插入图库,名称错误

Android 将图片插入图库,名称错误,android,image,insert,gallery,Android,Image,Insert,Gallery,我想把一张照片保存到图库中。它使用下面的代码,但图片中的名称是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

我想把一张照片保存到图库中。它使用下面的代码,但图片中的名称是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");

非常感谢您提供的任何帮助。

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

另一种方法是

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" /> 

谢谢,但这段代码让我回到了我的老问题:我希望图像与普通相机应用程序中的照片完全一样位于文件夹中,并且创建日期必须正确。据我所知,相机捕获的默认文件夹是DCIM。您也可以指定文件夹名称。答案已解决,但我没有意识到名称现在是错误的。您的答案是正确的,但图像不会立即显示在库中。如果添加以下内容:exploreActivity.sendBroadcast(新意图(Intent.ACTION\u MEDIA\u MOUNTED,Uri.parse(“文件:/”+Environment.getExternalStorageDirectory()));(将图像立即添加到图库)对于您的答案,我会将其标记为正确。@lukas更新的答案。mScreenshotPath的值是私有字符串mScreenshotPath=Environment.getExternalStorageDirectory()+“/foldername”;
 exploreActivity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); 
 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);    
                    }                                          
                }