存储和SD卡闪存
存储是将你想输入的数据存入到你的activity的文件当中然后可以使用,但是当文件删除时也会消失,所以只可以是APP之间的使用.
SD卡闪存是将数据数据存储到手机里当你的APP被删除时数据还会被保留,只要手机还在那么你存入的数据便可以被使用.
package com.example.sd008;
import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class MainActivity extends AppCompatActivity {
private Button buttonread,buttonwrite;
private TextView textView;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonread=findViewById(R.id.btn_read);
buttonwrite=findViewById(R.id.btn_read);
textView=findViewById(R.id.main_text);
imageView=findViewById(R.id.main_image);
buttonread.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_APN_SETTINGS},10086);
}
});
buttonwrite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File file = Environment.getExternalStorageDirectory();
File file1 = new File(file, "vvv.jpg");
Bitmap bitmap = BitmapFactory.decodeFile(file1.getAbsolutePath());
imageView.setImageBitmap(bitmap);
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==10086&&grantResults[0]== PackageManager.PERMISSION_GRANTED){
new MyAsy(MainActivity.this,imageView,textView).execute("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1565414228257&di=ee5bfd1ad0aa1f155d494200ae6fe807&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F6f4132940988c009ed5b02af106830626fb7ccc8dc8b-WIrvkD_fw658");
}
}
}
package com.example.sd008;
import android.Manifest;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.net.ssl.HttpsURLConnection;
public class MyAsy extends AsyncTask<String,String,String> {
private Context context;
private ImageView imageView;
private TextView textView;
public MyAsy(Context context, ImageView imageView, TextView textView) {
this.context = context;
this.imageView=imageView;
this.textView=textView;
}
@Override
protected String doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
File file = Environment.getExternalStorageDirectory();
FileOutputStream out = new FileOutputStream(new File(file, "vvv.jpg"));
if (connection.getResponseCode()==200){
InputStream inputStream = connection.getInputStream();
int len=0;
byte[] b=new byte[1024];
while((len=inputStream.read(b))!=-1){
out.write(b,0,len);
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(context, "下载成功", Toast.LENGTH_SHORT).show();
}
}