将文件保存在内部存储上

时间:2016-09-08 08:24:04

标签: java android storage

我很难将文件保存到内部存储中。下面是我保存到外部存储的代码,但我不知道将文件保存到内部存储的正确方法。我是新编码的人。任何人都可以帮助我。

public class Semester extends AppCompatActivity {

  Button btnSem;
  ListView lvFiles;
  private ArrayAdapter arrayAdapter;
  private ArrayList < String > files_on_server = new ArrayList < > ();
  private Handler handler;
  private String selected_file;
  private ProgressDialog progressDialog;

  @
  Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_semester);
    btnSem = (Button) findViewById(R.id.btnSem);
    lvFiles = (ListView) findViewById(R.id.lvFiles);

    initialize();

  }

  private void initialize() {

    arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, files_on_server);
    lvFiles.setAdapter(arrayAdapter);
    handler = new Handler();
    progressDialog = new ProgressDialog(this);
    progressDialog.setTitle("Downloading...");
    progressDialog.setMax(100);
    progressDialog.setCancelable(false);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);


    btnSem.setOnClickListener(new View.OnClickListener() {@
      Override
      public void onClick(View view) {
        Thread t = new Thread(new Runnable() {@
          Override
          public void run() {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder().url("http:/192.168.0.102/client/index.php?list_files").build();
            Response response = null;
            try {
              response = client.newCall(request).execute();
              JSONArray array = new JSONArray(response.body().string());

              for (int i = 0; i < array.length(); i++) {
                String file_name = array.getString(i);
                if (files_on_server.indexOf(file_name) == -1)
                  files_on_server.add(file_name);
              }
              handler.post(new Runnable() {@
                Override
                public void run() {
                  arrayAdapter.notifyDataSetChanged();
                }
              });


            } catch (IOException e) {
              e.printStackTrace();
            } catch (JSONException e) {
              e.printStackTrace();
            }
          }
        });
        t.start();
      }

    });
    lvFiles.setOnItemClickListener(new AdapterView.OnItemClickListener() {@
      Override
      public void onItemClick(AdapterView <? > adapterView, View view, int i, long l) {
        selected_file = ((TextView) view).getText().toString();

        Thread t = new Thread(new Runnable() {@
          Override
          public void run() {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder().url("192.168.0.102/client/" + selected_file).build();

            Response response = null;
            try {
              response = client.newCall(request).execute();
              float file_size = response.body().contentLength();

              BufferedInputStream inputStream = new BufferedInputStream(response.body().byteStream());
              OutputStream stream = new FileOutputStream(Environment.getRootDirectory() + "/files" + selected_file);
              byte[] data = new byte[8192];
              float total = 0;
              int read_bytes = 0;

              handler.post(new Runnable() {@
                Override
                public void run() {
                  progressDialog.show();
                }
              });

              while ((read_bytes = inputStream.read(data)) != -1) {
                total = total + read_bytes;
                stream.write(data, 0, read_bytes);
                progressDialog.setProgress((int)(total / file_size) * 100);

              }
              progressDialog.dismiss();
              stream.flush();
              stream.close();
              response.body().close();


            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        });
      }
    });

  }



}

`

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

var myApp = angular.module("myApp",[]);
myApp.controller('MasterDetailCtrl',function ($scope,$http,emailService,$q) {

var promises = new Array();    
var data = [1,2,3,4];

$scope.multipleCalls = function(){
    angular.forEach(data, function (item, index) {  
            promises.push( emailService.getEmails(data[index]));
        }); 


$q.all(promises).then(function(data) {
        apiResponseArray = data;
        var found = false;             
        angular.forEach(apiResponseArray,function (item1, index1) {
                console.log(angular.toJson(item1));
         });
     });
    }
}); 

myApp.factory('emailService', function ($http, $q) {
return {
    getEmails: function(id) {
        return $http.get('https://users.tap.sanv.com/api/find/?q=email:'+id)
            .then(function(response) {
                if (typeof response.data === 'object') {
                    return response.data;
                } else {
                    // invalid response
                    return $q.reject(response.data);
                }

            }, function(response) {
                // something went wrong
                return $q.reject(response.data);
            });
    }
  };
});

在上面一行中,您应该使用OutputStream stream = new FileOutputStream(Environment.getRootDirectory() + "/files" + selected_file); Environment.getRootDirectory()的{​​/ 1}} instad存储在您的应用程序内部目录中。

您也可以使用getFilesDir()

直接创建FileOutputStream