0

I'm trying to make a really simple app which allow users write on the image provided and save it to their gallery. And I tried the code below, and it can't be saved. Could you tell me which part I need to edit?

 ImageView imagecp = (ImageView) findViewById(R.id.imageView1); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.actionbar, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()){ case R.id.savebutton: Bitmap bitmap = ((BitmapDrawable)imagecp.getDrawable()).getBitmap(); Save savefile = new Save(); savefile.SaveImage(this, bitmap); Toast.makeText(getApplicationContext(),"image saved", Toast.LENGTH_SHORT).show(); default: return super.onOptionsItemSelected(item); } 

public class Save { private Context TheThis; private String NameOfFolder = "/Amir_Paint"; private String NameOfFile = "APaintImage"; public void SaveImage(Context context,Bitmap ImageToSave){ TheThis = context; String file_path = Environment.getExternalStorageDirectory().getAbsolutePath()+ NameOfFolder; String CurrentDateAndTime= getCurrentDateAndTime(); File dir = new File(file_path); if(!dir.exists()){ dir.mkdirs(); } File file = new File(dir, NameOfFile +CurrentDateAndTime+ ".jpg"); try { FileOutputStream fOut = new FileOutputStream(file); ImageToSave.compress(Bitmap.CompressFormat.JPEG, 85, fOut); fOut.flush(); fOut.close(); MakeSureFileWasCreatedThenMakeAvabile(file); AbleToSave(); } catch (FileNotFoundException e) {UnableToSave();} catch (IOException e){UnableToSave();} } private void MakeSureFileWasCreatedThenMakeAvabile(File file) { MediaScannerConnection.scanFile(TheThis, new String[] { file.toString() }, null, new MediaScannerConnection.OnScanCompletedListener() { public void onScanCompleted(String path, Uri uri) { Log.e("ExternalStorage", "Scanned " + path + ":"); Log.e("ExternalStorage", "-> uri=" + uri); } }); } private String getCurrentDateAndTime() { Calendar c = Calendar.getInstance(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); String formattedDate = df.format(c.getTime()); return formattedDate; } private void UnableToSave() { Toast.makeText(TheThis, "Picture cannot saved to gallery", Toast.LENGTH_SHORT).show(); } private void AbleToSave() { Toast.makeText(TheThis, "Picture saved successfully", Toast.LENGTH_SHORT).show(); } 

}

And error message below.

 09-30 14:30:30.311 3847-3863 W/EGL_emulation﹕ eglSurfaceAttrib not implemented 09-30 14:30:30.311 3847-3863/ W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xad761380, error=EGL_SUCCESS 09-30 14:30:32.259 3847-3863/ E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xab79e440 09-30 14:30:38.354 3847-3847/ W/art﹕ Long monitor contention event with owner method=void android.os.MessageQueue.nativeWake(long) from MessageQueue.java:4294967294 waiters=0 for 618ms 09-30 14:30:38.389 3847-4029/ W/art﹕ Long monitor contention event with owner method=void android.os.MessageQueue.nativeWake(long) from MessageQueue.java:4294967294 waiters=1 for 651ms 09-30 14:30:38.461 3847-3863/ E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xab79e360 09-30 14:32:33.654 3847-3854/ W/art﹕ Suspending all threads took: 16.292ms 

This is the permission I put on menifest file.

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
3
  • 1
    you should share your error messages as well. Commented Sep 30, 2016 at 20:34
  • dir.mkdirs();. Check the return value as it might fail. If false display a toast and return. You also could use .exists() again to see if the directory is created. Commented Oct 1, 2016 at 5:15
  • "Picture cannot saved to gallery". That is not true. You could not save to a folder in external memory. Has nothing to do with the Gallery app or MediaStore. Moreover you should tell the user to which folder you tried to write a file. Use e.getMessage() when there is a catch. Commented Oct 1, 2016 at 5:18

2 Answers 2

1

You should set the view instance inside of onCreateView method.

This is wrong.

ImageView imagecp = (ImageView) findViewById(R.id.imageView1); 

This is the right:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.testclassfragment, container, false); imagecp = (ImageView)v.findViewById(R.id.imageView1); return v }} 

change this

String file_path = Environment.getExternalStorageDirectory().getAbsolutePath()+ NameOfFolder; 

with

String file_path = Environment.getExternalStorageDirectory().toString() + NameOfFolder; 
Sign up to request clarification or add additional context in comments.

12 Comments

I just put imagecp = (ImageView) findViewById(R.id.imageView1); this code inside onCreate method. But keep ImageView imagecp; this part outside so that I can reuse imagecp later on the code. And that works.
@JessJ thats why I called the instance. en.wikipedia.org/wiki/Instance_variable
app doesn't crush, but cannot save the image.(IOException). Is there any other part I'm doing wrong? I added new error message.
@JessJ please be sure whether the bitmap is null or not. You can also share the code line that imagecp is set the bitmap
Bitmap bitmap = ((BitmapDrawable)imagecp.getDrawable()).getBitmap(); Save savefile = new Save(); savefile.SaveImage(this, bitmap);
|
0

Please consider using MediaStore insertImage instead of this old code.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.