2015-06-10 66 views
0

我有一个要求,在Android应用程序中存储图像,它不应该出现在画廊。所以,我决定在应用程序的“资产”文件夹中有SQLite数据库,我会将图像路径存储到数据库中。问题是,如果没有SDCard,那么我如何获得图像路径?或者有没有一种方法可以隐藏我的应用拍摄的图像出现在画廊中。以下是我目前用来将图像存储在外部目录中的代码。图像到SQLite,而不是画廊

photo = new File(Environment.getExternalStoragePublicDirectory(Environment .DIRECTORY_PICTURES), imageName); 
//imageName=current timestamp 

回答

0

我用这个方法将图像保存到内部存储(和返回的路径保存到您的sqlite)使用

private String saveToInternalStorage(Bitmap bitmapImage, String filename){ 
    ContextWrapper cw = new ContextWrapper(getApplicationContext()); 
    // path to /data/data/yourapp/app_data/imageDir 
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE); 
    Log.d("dir", directory.toString()); 
    // Create imageDir 
    File mypath=new File(directory,filename); 
    Log.d("path", mypath.toString()); 

    FileOutputStream fos = null; 
    try { 

     fos = new FileOutputStream(mypath); 

     // Use the compress method on the BitMap object to write image to the OutputStream 
     bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos); 
     fos.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    Log.d("ingesteld path", directory.getAbsolutePath()); 
    return directory.getAbsolutePath(); 
} 

加载图像:

private void loadImageFromStorage(String path, String name) 
{ 
    try { 
     File f=new File(path, name); 
     Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f)); 

     // Do something with your bitmap 
    } 
    catch (FileNotFoundException e) 
    { 
     e.printStackTrace(); 
    } 

} 

希望这是有用的。

+0

谢谢你Opoo。我正在使用ByteArrayOutputStream而不是FileOutputStream来写入内存。是否可观?另外,你能帮我理解哪个路径被存储到数据库..是它的路径 - “/ data/data/yourapp/app_data/imageDir”? – Kittu

+1

我不知道它是否工作原理相同,在我的情况下,使用上面使用的方法存储的路径是“/data/data/com.example.app/app_imageDir”。 String path = saveToInternalStorage(bitmap,name); question.setImagePath(path); – Stefan