Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save Image From WebView

I will try to be short - so I'm making this app, where image is uploaded to custom Webview, draw canvas and save it. Now I successfully execute everything, up to the point of saving it. I've tried a lot of different methods, but none worked.

I save unedited image from Url with:

public void DownloadFromUrl(String fileName) {  //this is the downloader method     try {         URL url = new URL(wv2.getUrl()); //you can write here any link         File file = new File(fileName);         Canvas canvas = new Canvas();         wv2.draw(canvas );          long startTime = System.currentTimeMillis();                            Log.d("ImageManager", "download begining");         Log.d("ImageManager", "download url:" + url);         Log.d("ImageManager", "downloaded file name:" + fileName);         URLConnection ucon = url.openConnection();          InputStream is = ucon.getInputStream();         BufferedInputStream bis = new BufferedInputStream(is);          ByteArrayBuffer baf = new ByteArrayBuffer(50);         int current = 0;         while ((current = bis.read()) != -1) {             baf.append((byte) current);         }          FileOutputStream fos = new FileOutputStream(file);         fos.write(baf.toByteArray());         fos.close();         Log.d("ImageManager", "download ready in"                         + ((System.currentTimeMillis() - startTime) / 1000)                         + " sec");     } catch (IOException e) {         Log.d("ImageManager", "Error: " + e);     } } 

My draw function in custom webview looks like this:

@Override protected void onDraw(Canvas canvas) {     super.onDraw (canvas);     /*code that draws different stuff*/ } 

To display edited image I think I need to use this:

canvas = new Canvas(mutableBitmap);                  wv2.draw(canvas); tstImage.setImageBitmap(mutableBitmap); 

But again, I don't know how to merge canvas into the image. Should I use Drawing Cache? If yes, then how?

[UPDATE]

Okay, so I've managed to save the edited image using this code

wv2.setDrawingCacheEnabled(true); Bitmap bitmap = Bitmap.createBitmap(wv2.getDrawingCache()); wv2.setDrawingCacheEnabled(false); tstImage.setImageBitmap(bitmap); SaveImage(bitmap); 

Where SaveImage:

private void SaveImage(Bitmap finalBitmap) {     String root = Environment.getExternalStorageDirectory().toString();     File myDir = new File(root + "/Pictures/");         myDir.mkdirs();      String fname = "Image-"+ count +".jpg";     File file = new File (myDir, fname);     if (file.exists ()) file.delete ();      try {         FileOutputStream out = new FileOutputStream(file);         finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);         out.flush();         out.close();     } catch (Exception e) {         e.printStackTrace();     } } 

My only problem is that the image I get is what was shown in the webview at that moment. So if I zoom the image, it saves only a piece of it.

[UPDATE 2]

I've modified one of the answers in search for a solution, this is what I've written. No image is getting displayed.

   File imageFile = new File("file://" +         Environment.getExternalStorageDirectory() + "/Pictures/" + count + ".jpg");                         String location = new String("file://" + Environment.getExternalStorageDirectory() + "/Pictures/" + count + ".jpg");                         if(imageFile.exists()){                             Bitmap myBitmap = BitmapFactory.decodeFile(location);                             Bitmap mutableBitmap = myBitmap.copy(Bitmap.Config.ARGB_8888, true);                             Canvas canvas = new Canvas(mutableBitmap);                             wv2.draw(canvas);                             tstImage.setImageBitmap(mutableBitmap);                         } 
like image 842
Oleksandr Firsov Avatar asked Dec 09 '15 19:12

Oleksandr Firsov


1 Answers

try this

Bitmap workingBitmap = Bitmap.createBitmap(chosenFrame); Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true); Canvas canvas = new Canvas(mutableBitmap); 
like image 147
sKv Avatar answered Oct 02 '22 10:10

sKv