0

現在、次の方法を使用して、XML ファイルを mode_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();
    }

}

URL から約 5kb の小さな画像を、mode_private のアプリケーション スペースに保存したいと考えています。mode_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();
    }
4

2 に答える 2

7

だから私はついにmode_privateで画像を保存する方法を考え出しました

以下の方法では、ビットマップを取得して、mode_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);

これが誰かに役立つことを願っています。

于 2013-03-17T21:36:48.057 に答える