Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.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_File - Fatal编程技术网

Android 缓存现有文件

Android 缓存现有文件,android,file,Android,File,我有以下代码: file = new File(getRealPathFromURI(uri)); 我怎样才能用一个名字将它存储在缓存中,以便以后访问它 我知道有一些方法,比如File outputDir=context.getCacheDir(); File outputFile=File.createTempFile(“前缀”,“扩展名”,outputDir) 但我不明白如何使用特定名称将此文件存储在缓存中,以便在以后的某个日期执行file=new file(getActivity().g

我有以下代码:

file = new File(getRealPathFromURI(uri));
我怎样才能用一个名字将它存储在缓存中,以便以后访问它

我知道有一些方法,比如
File outputDir=context.getCacheDir();
File outputFile=File.createTempFile(“前缀”,“扩展名”,outputDir)

但我不明白如何使用特定名称将此文件存储在缓存中,以便在以后的某个日期执行
file=new file(getActivity().getCacheDir(),“storedFileName”)在其他活动中

任何指导都很好,谢谢

编辑: 这是我的主要活动,我从图库中获取了一张图片,并在onActivityResult中作为uri返回:

@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    switch (requestCode) {
    case SELECT_PHOTO:
        if (resultCode == RESULT_OK) {
            Uri selectedImage = imageReturnedIntent.getData();

            Intent i = new Intent(getApplicationContext(),
                    sliding_menu.class);


            File file = new File(selectedImage.getPath());

              ObjectOutput out;
                try {
                    String filenameOffer="Image";

                    out = new ObjectOutputStream(new FileOutputStream(new File
                            (getCacheDir(),"")+filenameOffer));
                    out.writeObject(file);
                    out.close();
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } 
            startActivity(i);

        }
    }
}
如您所见,我正在尝试生成所选图像的Uri,然后将其生成一个文件

然后我尝试将文件存储在缓存中,以便在整个应用程序中进一步检索它

下面是我尝试访问文件的下一个活动:

try {
            String filename="Image";

            ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(new File(
                    getActivity().getCacheDir(),"")+filename)));
            String res =  (String) in.readObject();



            Picasso.with(getActivity().getApplication()).load((res))
            .into(mImageView);

        } catch (Exception e) {


        }

但是图像没有加载。我可以更改什么使其工作?

带有.png文件的示例

保存文件:(InputStream=来自internet)

读取文件:

 File path = new File(Environment.getExternalStorageDirectory(),"MyApp/" + fileName);

        if(path.exists())
        {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bitmap = BitmapFactory.decodeFile(path.getAbsolutePath(), options);
        }

要在缓存文件中存储数据,可以使用

假设响应是要存储在缓存中的字符串

           ObjectOutput out;
            try {
                String filenameOffer="cacheFileSearch.srl";

                out = new ObjectOutputStream(new FileOutputStream(new File
                        (getActivity().getCacheDir(),"")+filenameOffer));
                out.writeObject( response );
                out.close();
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } 
为了从缓存文件中获取数据

    try {
        String filename="cacheFileSearch.srl";

        ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(new File(
                getActivity().getCacheDir(),"")+filename)));
        String res =  (String) in.readObject();

    } catch (Exception e) {
        AppConstant.isLoadFirstTime=true;
    }
对于删除文件,您可以使用

        String filename="cacheFileSearch.srl";

        try {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(new File(
                    getActivity().getCacheDir(),"")+filename)));
            File dir = getActivity().getCacheDir();
            if (dir.isDirectory()) {
                if (new File(new File(dir, "")+filename).delete()) {
                }

            } 
            in.close();

        } catch (Exception e) {

        }

您提供的代码在我的实例中不起作用。我已经用我想做的更新了我的问题,谢谢。谢谢,它有效,还有一个问题-有没有办法加快速度?也许存储uri而不是位图?
        String filename="cacheFileSearch.srl";

        try {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(new File(
                    getActivity().getCacheDir(),"")+filename)));
            File dir = getActivity().getCacheDir();
            if (dir.isDirectory()) {
                if (new File(new File(dir, "")+filename).delete()) {
                }

            } 
            in.close();

        } catch (Exception e) {

        }