Reputation: 35
In here i want to save my autogenerate qrcode to my internal memory.but, i can't save my image
i already try much code but from base64string to bitmap. but, still i can't save my image.
this is my confirmation.java
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();
}
}
}
in here i already get bitmap of qrcode and i want to save qrcode to my internal mobile storage
Upvotes: 0
Views: 5077
Reputation: 765
Do you add <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in AndroidManifest file ?
UPDATE: if you want to save to internal storage, you can use below code:
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();
}
}
it will save to data/data/your_packagename/files
Upvotes: 1
Reputation: 309
You can use below code to save bitmap to internal storage.It will create dirctory in internal storage imageDir
, and you have to give the name to save your image i.e profile.jpg
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();
}
Upvotes: 1