Reputation: 21
I made an drawing application in android studio. how can I save the drawing in the internal storage and view it afterwards in another activity? I've got some codes in the internet on how to save the bitmap but it won't work and I don't what I've done wrong. I am already stuck in this part.
Here are some of the codes:
public onClick(View v){
if(v.getId() == R.id.btnBrush){
.....
}
else if(v.getId() == R.id.btnErase){
.....
}
else if(v.getId() == R.id.ibtnSave){
//save button clicked
final Dialog saveDialog = new Dialog(this);
saveDialog.setTitle("Save");
saveDialog.setContentView(R.layout.save_form);
saveDialog.show();
inputName = (EditText) findViewById(R.id.drawFile);
inputDate = (EditText) findViewById(R.id.date);
btnSave = (Button) saveDialog.findViewById(R.id.btnSave);
btnCancel = (Button) saveDialog.findViewById(R.id.btnCancel);
btnSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
iView.setDrawingCacheEnabled(true);
save();
saveDialog.dismiss();
Intent intent = new Intent(DrawingPanel.this, Gallery.class);
startActivity(intent);
finish();
iView.destroyDrawingCache();
}
});
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
saveDialog.dismiss();
}
});
}
}
public void save(){
Bitmap bitmap = iView.getDrawingCache();
try{
FileOutputStream fos = openFileOutput(inputName.getText().toString() + ".png", Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
if(fos != null){
Toast saved = Toast.makeText(getApplicationContext(), "Image saved.", Toast.LENGTH_SHORT);
saved.show();
}
else{
Toast unsaved = Toast.makeText(getApplicationContext(), "Image not save.", Toast.LENGTH_SHORT);
unsaved.show();
}
fos.close();
}
catch(Exception e){
Log.e("save()", e.getMessage());
}
}
I want to add the date on it as well but I just don't know how to do it. I am really sorry guys. I'm just new in android studio and I am not really good at this that's why I need help cause I really want to learn.
Upvotes: 0
Views: 5892
Reputation: 338
public void save(){
Bitmap bitmap = iView.getDrawingCache();
try{
if(bitmap!=null){
File file=new File(Environment.getExternalStorageDirectory()+"/dirr");
if(!file.isDirectory()){
file.mkdir();
}
file=new File(Environment.getExternalStorageDirectory()+"/dirr",inputName.getText()+"_"+System.currentTimeMillis()+".jpg");
try
{
FileOutputStream fileOutputStream=new FileOutputStream(file);
if(bitmap.compress(Bitmap.CompressFormat.JPEG,100, fileOutputStream))
{
Toast saved = Toast.makeText(getApplicationContext(), "Image saved.", Toast.LENGTH_SHORT);
saved.show();
}
else{
Toast unsaved = Toast.makeText(getApplicationContext(), "Image not save.", Toast.LENGTH_SHORT);
unsaved.show();
}
}
catch(IOException e){
e.printStackTrace();
}
finally {
try {
if (fileOutputStream!= null) {
// fileOutputStream.flush();
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
catch(Exception e){
Log.e("save()", e.getMessage());
}
}
And add permissions in manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
EDIT: Just cleaned the code a bit, and also you can take a look on similar questions:
Save Bitmap into File and return File having bitmap image
How to save a bitmap on internal storage
Upvotes: 1