Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.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
Java 我可以在私人模式下保存图像吗?_Java_Android_Url_Download - Fatal编程技术网

Java 我可以在私人模式下保存图像吗?

Java 我可以在私人模式下保存图像吗?,java,android,url,download,Java,Android,Url,Download,当前,我使用以下方法将XML文件保存在模式\u private中: public void Save_Data(String filename, String Datastring, Context context) { FileOutputStream fos; try { fos = context.openFileOutput(filename, context.MODE_PRIVATE); fos.write(Datastring.get

当前,我使用以下方法将XML文件保存在模式\u private中:

public void Save_Data(String filename, String Datastring, Context context) {
    FileOutputStream fos;

    try {
        fos = context.openFileOutput(filename, context.MODE_PRIVATE);
        fos.write(Datastring.getBytes());
        fos.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
我想在mode_private中将一个大约5kb的小图像从URL保存到我的应用程序空间中。是否可以在模式_private下保存图像?如果是这样的话,有人能告诉我如何修改下面的方法来做到这一点吗

public void DownloadFromUrl(final String DownloadUrl, final String fileName) {


    Thread web = new Thread(){
        public void run(){


       try {
               File root = Environment.getExternalStorageDirectory();               

               File dir = new File (root.getAbsolutePath() + "/Chats");
               if(dir.exists()==false) {
                    dir.mkdirs();
               }

               URL url = new URL(DownloadUrl); //you can write here any link
               File file = new File(dir, fileName);

               long startTime = System.currentTimeMillis();
               Log.d("DownloadManager", "download begining");
               Log.d("DownloadManager", "download url:" + url);
               Log.d("DownloadManager", "downloaded file name:" + fileName);

               /* Open a connection to that URL. */
               URLConnection ucon = url.openConnection();

               /*
                * Define InputStreams to read from the URLConnection.
                */
               InputStream is = ucon.getInputStream();
               BufferedInputStream bis = new BufferedInputStream(is);

               /*
                * Read bytes to the Buffer until there is nothing more to read(-1).
                */
               ByteArrayBuffer baf = new ByteArrayBuffer(5000);
               int current = 0;
               while ((current = bis.read()) != -1) {
                  baf.append((byte) current);
               }


               /* Convert the Bytes read to a String. */
               FileOutputStream fos = new FileOutputStream(file);
               fos.write(baf.toByteArray());
               fos.flush();
               fos.close();
               Log.d("DownloadManager", "download ready in " + ((System.currentTimeMillis() - startTime) / 1000) + " sec");

       } catch (IOException e) {
           Log.d("DownloadManager", "Error: " + e);
       }

        }
        };
        web.start();
    }

所以我终于想出了如何在mode_private中保存图像

下面的方法获取位图,并将其保存到您的应用程序私有空间中的模式_private

 public String writeFileToInternalStorage(Bitmap outputImage){
        String fileName = fileName + ".png";

        FileOutputStream fos = null;
  try {
   fos = openFileOutput(fileName, Context.MODE_PRIVATE);
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
        outputImage.compress(Bitmap.CompressFormat.PNG, 90, fos);

  return fileName;
    }
这将从应用程序专用空间读取图像,并将其转换为位图:

 Bitmap bitmap = BitmapFactory.decodeFile(this.getApplicationContext().getFilesDir() + "/"+fileName);

希望这对别人有帮助

根据Randrm的回答,我实现了以下两种方法来存储/检索应用程序私有数据空间中ImageView中的位图图像。基本上是他的答案的一个清理版本,取自编译代码

如果希望您的活动在恢复时快速恢复其视图内容,则可能会很有用。ImageView由R.string.myImage标识,该位图通过类的myBitmap成员访问

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // we will be stopped and perhaps later on destroyed;
    // store displayed image for quick retrieval in private data space
    try {
        FileOutputStream output =
           openFileOutput(this.getString(R.string.myImage), Context.MODE_PRIVATE);
        myBitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
    } catch (Exception e) {
        Log.e(LOG_TAG, "could not save image to private space");
    }

    // superclass to save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    // superclass to restore the view hierarchy
    super.onRestoreInstanceState(savedInstanceState);

    // retrieve image stored earlier
    try {
        FileInputStream input = openFileInput(this.getString(R.string.myImage));
        ((ImageView) findViewById(R.id.det_mainimage)).
          setImageBitmap(BitmapFactory.decodeStream(input));            
    } catch (Exception e) {
        Log.e(LOG_TAG, "could not retrieve image from private space");
    }
}
享受