Android 如何将位图保存到内部存储器?
在这里,我想将自动生成的qrcode保存到内存中。但是,我无法保存图像 我已经尝试了很多代码,但从base64string到位图。但是,我仍然无法保存我的图像 这是我的确认书Android 如何将位图保存到内部存储器?,android,image,android-internal-storage,Android,Image,Android Internal Storage,在这里,我想将自动生成的qrcode保存到内存中。但是,我无法保存图像 我已经尝试了很多代码,但从base64string到位图。但是,我仍然无法保存我的图像 这是我的确认书 private static final int REQUEST_CAMERA_PERMISSION = 200; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
private static final int REQUEST_CAMERA_PERMISSION = 200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirmasi);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
ActivityCompat.requestPermissions(this,new String[]{
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE
},REQUEST_CAMERA_PERMISSION);
if(PendaftaranDokter.message == "null") {
Date todayDate = Calendar.getInstance().getTime();
SimpleDateFormat formatter = new SimpleDateFormat("d/M/yyyy");
String todayString = formatter.format(todayDate);
msgcin.setText(todayString);
NoAntrian.setText("xxxxx");
if(PendaftaranDokterAfterPoli.tgljanjian1.equals(todayString)) {
//new MedNo(Login.username1).execute();
new Regno(PendaftaranDokterAfterPoli.medno).execute();
msgcin.setText("Registrasi");
NoAntrian.setText("xxxxx");
}
else {
//new MedNo(Login.username1).execute();
new AppNo(PendaftaranDokterAfterPoli.medno).execute();
msgcin.setText("Appointment");
NoAntrian.setText("-");
}
medno.setText(PendaftaranDokterAfterPoli.medno);
namadokter.setText(PendaftaranDokterAfterPoli.parname1);
politujuan.setText(PendaftaranPoli.servname1);
try {
String date = PendaftaranDokterAfterPoli.tgljanjian1;
SimpleDateFormat format = new SimpleDateFormat("d/M/yyyy");
Date newDate = format.parse(date);
format = new SimpleDateFormat("dd MMMM yyyy");
String date1 = format.format(newDate);
tanggal.setText(date1);
}catch (Exception e)
{
Toast.makeText(Confirmasi.this,e.toString(),Toast.LENGTH_SHORT).show();
}
}
else
{
Date todayDate = Calendar.getInstance().getTime();
SimpleDateFormat formatter = new SimpleDateFormat("d/M/yyyy");
String todayString = formatter.format(todayDate);
if (PendaftaranDokter.tgljanjian.equals(todayString)) {
//new MedNo(Login.username1).execute();
new Regno(PendaftaranDokter.medno).execute();
msgcin.setText("Registrasi");
NoAntrian.setText("xxxx");
} else {
//new MedNo(Login.username1).execute();
new AppNo(PendaftaranDokter.medno).execute();
msgcin.setText("Appointment");
NoAntrian.setText("-");
}
medno.setText(PendaftaranDokter.medno);
namadokter.setText(PendaftaranDokter.parname);
politujuan.setText(PendaftaranDokter.servname);
try {
String date = PendaftaranDokter.tgljanjian;
SimpleDateFormat format = new SimpleDateFormat("d/M/yyyy");
Date newDate = format.parse(date);
format = new SimpleDateFormat("dd MMMM yyyy");
String date1 = format.format(newDate);
tanggal.setText(date1);
}catch (Exception e)
{
Toast.makeText(Confirmasi.this,e.toString(),Toast.LENGTH_SHORT).show();
}
}
ok = (ImageView) findViewById(R.id.ok);
ok.setImageResource(R.drawable.confirmimage);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OpenMainMenu();
}
});
}
private File getOutputMediaFile(){
File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
+ "/Android/data/"
+ getApplicationContext().getPackageName()
+ "/Files");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
File mediaFile;
String mImageName="MI_"+ timeStamp +".jpg";
mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
return mediaFile;
}
private void storeImage(Bitmap image) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
Log.d(TAG, "Error creating media file, check storage permissions: ");// e.getMessage());
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
image.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
class Regno extends AsyncTask<String, String, String>
{
private String medno;
public Regno(String medno) {
this.medno = medno;
}
@Override
protected String doInBackground(String... strings) {
CallSoap cs = new CallSoap();
String medno1 = cs.RegNo(medno);
return medno1;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
try {
BitMatrix bitMatrix = multiFormatWriter.encode(s, BarcodeFormat.QR_CODE, 200, 200);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
img.setImageBitmap(bitmap);
storeImage(bitmap);
//saveToInternalStorage(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
number.setText(s);
}
}
class AppNo extends AsyncTask<String, String, String>
{
private String medno;
public AppNo(String medno) {
this.medno = medno;
}
@Override
protected String doInBackground(String... strings) {
CallSoap cs = new CallSoap();
String medno1 = cs.AppointNo(medno);
return medno1;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
try {
BitMatrix bitMatrix = multiFormatWriter.encode(s, BarcodeFormat.QR_CODE, 100, 100);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
img.setImageBitmap(bitmap);
storeImage(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
}
}
private static final int REQUEST\u CAMERA\u PERMISSION=200;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirmasi);
setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u Picture);
ActivityCompat.requestPermissions(此,新字符串[]){
舱单,许可,摄像机,
Manifest.permission.WRITE\u外部存储
},请求(摄像机许可);
if(PendaftaranDokter.message==“null”){
datetodaydate=Calendar.getInstance().getTime();
SimpleDataFormat格式化程序=新的SimpleDataFormat(“d/M/yyyy”);
String todayString=formatter.format(todayDate);
msgcin.setText(今日字符串);
NoAntrian.setText(“xxxxx”);
if(PendaftaranDokterAfterPoli.tgljanjian1.equals(todayString)){
//新的MedNo(Login.username1.execute();
新的Regno(PendaftaranDokterAfterPoli.medno).execute();
msgcin.setText(“注册”);
NoAntrian.setText(“xxxxx”);
}
否则{
//新的MedNo(Login.username1.execute();
新的AppNo(PendaftaranDokterAfterPoli.medno).execute();
msgcin.setText(“任命”);
NoAntrian.setText(“-”);
}
medno.SETEXT(PENDAFTRANDOKTERAFTERPOLI.medno);
namadokter.setText(PendaftaranDokterAfterPoli.parname1);
politiujuan.setText(PendaftaranPoli.servname1);
试一试{
字符串日期=PendaftaranDokterAfterPoli.tgljanjian1;
SimpleDateFormat=新的SimpleDateFormat(“d/M/yyyy”);
Date newDate=format.parse(日期);
格式=新的SimpleDataFormat(“dd MMMM yyyy”);
字符串date1=format.format(newDate);
tanggal.setText(日期1);
}捕获(例外e)
{
Toast.makeText(Confirmasi.this,e.toString(),Toast.LENGTH_SHORT.show();
}
}
其他的
{
datetodaydate=Calendar.getInstance().getTime();
SimpleDataFormat格式化程序=新的SimpleDataFormat(“d/M/yyyy”);
String todayString=formatter.format(todayDate);
if(PendaftaranDokter.tgljanjian.equals(todayString)){
//新的MedNo(Login.username1.execute();
新的Regno(PendaftaranDokter.medno).execute();
msgcin.setText(“注册”);
NoAntrian.setText(“xxxx”);
}否则{
//新的MedNo(Login.username1.execute();
新的AppNo(PendaftaranDokter.medno).execute();
msgcin.setText(“任命”);
NoAntrian.setText(“-”);
}
medno.setText(PendaftaranDokter.medno);
namadokter.setText(PendaftaranDokter.parname);
politiujuan.setText(PendaftaranDokter.servname);
试一试{
字符串日期=PendaftaranDokter.tgljanjian;
SimpleDateFormat=新的SimpleDateFormat(“d/M/yyyy”);
Date newDate=format.parse(日期);
格式=新的SimpleDataFormat(“dd MMMM yyyy”);
字符串date1=format.format(newDate);
tanggal.setText(日期1);
}捕获(例外e)
{
Toast.makeText(Confirmasi.this,e.toString(),Toast.LENGTH_SHORT.show();
}
}
ok=(ImageView)findViewById(R.id.ok);
ok.setImageResource(R.drawable.confirmimage);
ok.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
OpenMainMenu();
}
});
}
私有文件getOutputMediaFile(){
File mediaStorageDir=新文件(Environment.getExternalStorageDirectory()
+“/Android/data/”
+getApplicationContext().getPackageName()
+“/文件”);
如果(!mediaStorageDir.exists()){
如果(!mediaStorageDir.mkdirs()){
返回null;
}
}
//创建媒体文件名
字符串时间戳=新的SimpleDataFormat(“ddMMyyyy_HHmm”).format(新日期());
文件媒体文件;
字符串mimagname=“MI_933;”+时间戳+”.jpg”;
mediaFile=新文件(mediaStorageDir.getPath()+File.separator+mImageName);
返回媒体文件;
}
私有void存储图像(位图图像){
File pictureFile=getOutputMediaFile();
如果(pictureFile==null){
Log.d(标记“创建媒体文件时出错,请检查存储权限:”;//e.getMessage());
返回;
}
试一试{
FileOutputStream fos=新的FileOutputStream(pictureFile);
image.compress(Bitmap.CompressFormat.PNG,90,fos);
fos.close();
}catch(filenotfounde异常){
Log.d(标记“未找到文件:”+e.getMessage());
}捕获(IOE异常){
Log.d(标记“访问文件时出错:”+e.getMessage());
}
}
类Regno扩展了异步任务
{
私人字符串medno;
公共注册号(字符串编号){
this.medno=medno;
}
@凌驾
受保护的字符串背景(字符串…字符串){
CallSoap cs=新的CallSoap();
字符串medno1=cs.RegNo(medno);
返回medno1;
}
@凌驾
受保护的void onPostExecute(字符串s){
super.onPostExecute(s);
MultiFormatWriter MultiFormatWriter=新的MultiFormatWriter();
试一试{
BitMatrix BitMatrix=多格式编写器编码(条形码格式QR_码,200200);
BarcodeCoder BarcodeCoder=新的BarcodeCoder();
位图位图=条形码编码器。创建位图(位图矩阵);
设置图像位图(位图);
存储图像(位图);
//存储到内部存储器(位图);
}捕获(写入异常e){
e、 printStackTrace();
}
编号.setText(s);
}
}
cla
private String saveToInternalStorage(Bitmap bitmapImage){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,"profile.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return directory.getAbsolutePath();
}
private void storeImage(Bitmap image) {
try {
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
String mImageName="MI_"+ timeStamp +".jpg";
FileOutputStream fos = openFileOutput(mImageName, MODE_PRIVATE);
image.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}