简体   繁体   English

在 Android 内部存储中缓存对象

[英]Caching Objects in Android Internal Storage

i am storing data in internal storage我将数据存储在内部存储器中

public static void writeObject(Context context, String key, Object object) throws IOException {
  FileOutputStream fos = context.openFileOutput(key, Context.MODE_PRIVATE);
  ObjectOutputStream oos = new ObjectOutputStream(fos);
  oos.writeObject(object);
  oos.close();
  fos.close();

} }

but can i see this saved data on phone if yes path please ?但是如果是路径,我可以在手机上看到这个保存的数据吗? any way i can store where user can't see this我可以以任何方式存储用户看不到的地方

you can store the image in the Cache storage , it will be private, only from your application users can see the images您可以将图像存储在缓存存储中,它将是私有的,只有从您的应用程序用户可以看到图像

public static String saveToCacheMemory(Activity activity, Bitmap 

bitmapImage){

    SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.US);

    ContextWrapper cw = new ContextWrapper(activity);
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    // Create imageDir
    File mypath=new File(directory,mDateFormat.format(new Date())  + StaticVariables.imageFormat);

    Log.d(TAG, "directory: " + directory);

    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);
        Log.d(TAG, "bit exception: Success" );
    } catch (Exception e) {
        Log.d(TAG, "bit exception: " + e.getMessage());
        e.printStackTrace();
    } finally {
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
            Log.d(TAG, "io exce: " + e.getMessage());
        }
    }
    Log.d(TAG, "absolute path " + directory.getAbsolutePath());
    return mypath.getAbsolutePath();
}

if you have more doubts check my blog post如果您有更多疑问,请查看我的博客文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM