2017-06-24 36 views
0

在我的應用程序中,我將webview文本保存爲圖像文件並啓動共享功能以允許用戶共享文件,圖像也保存在圖像文件夾中。我可以確認它們確實通過文件瀏覽器出現在圖片文件夾中,但圖庫沒有更新。如何讓圖庫(和相關圖像查看器)確認新圖像的存在?

我用來拍攝圖像的代碼是

private void htmlCapture2() { 
    if (!checksd()) { 
     Log.e("sdcard", "failed sd card check"); 
     //we dont have permision so lets not continue 
     return; 
    } 
    if (!checkFolder()) { 
     Log.e("folder", "failed folder check"); 
     //we dont have a folder so lets not continue 
     return; 
    } 
    WebView webView = (WebView) findViewById(R.id.webView); 
    webView.setDrawingCacheEnabled(true); 
    webView.buildDrawingCache(); 
    Bitmap bm = screenshot(webView); 
    Intent share = new Intent(Intent.ACTION_SEND); 
    share.setType("image/png"); 
    ContentValues values = new ContentValues(); 
    values.put(MediaStore.Images.Media.TITLE,"title"); 
    values.put(MediaStore.Images.Media.MIME_TYPE,"image/png"); 
    Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values); 
    OutputStream outstream; 
    try { 
     outstream = getContentResolver().openOutputStream(uri); 
     bm.compress(Bitmap.CompressFormat.PNG,100,outstream); 
     outstream.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    share.putExtra(Intent.EXTRA_STREAM,uri); 
    share.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1); 
    startActivity(Intent.createChooser(share,"Share Image")); 
} 

public static Bitmap screenshot(WebView webView) { 
    try { 
     //float scale = webView.getScaleX(); 
     int height; 
     height = webView.getHeight(); 

     Bitmap bitmap = Bitmap.createBitmap(webView.getWidth(), height, Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(bitmap); 
     canvas.drawColor(0xffffffff); 
     webView.draw(canvas); 
     return bitmap; 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

花了我三天遠遠得到這個,甚至更多尋找一個解決方案的最新問題。
我知道我需要使用MediaScannerConnection,但它需要一個文件名,我沒有一個,因爲我沒有真正創建一個文件。 我見過的解決方案,說來掃描SD卡,但是這似乎是一個壞主意,當我想他們絕望的他們似乎無法與最新的SDK

任何幫助,不勝感激

+0

我從來不使用'insert()',因爲它沒有特別好記錄'MediaStore'在這裏要做什麼。我建議你將圖像寫入文件,然後按照「MediaScannerConnection」配方。 – CommonsWare

+0

我被告知使用mediastore這種方式是推薦的路線,但通過文件路徑並保存它已修復的圖像,謝謝。 –

回答

0

工作,你需要通知的Android MediaStore一個新的形象已經被添加,你可以做到這一點通過以下

public Uri addImageToMediaStore(ContentResolver mresolver, String imgType, File filepath) { 
    ContentValues Cvalues = new ContentValues(); 
    Cvalues.put(MediaStore.Images.Media.TITLE, "player"); 
    Cvalues.put(MediaStore.Images.Media.DISPLAY_NAME, "player"); 
    Cvalues.put(MediaStore.Images.Media.DESCRIPTION, ""); 
    Cvalues.put(MediaStore.Images.Media.MIME_TYPE, "image/" + imgType); 
    Cvalues.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis()); 
    Cvalues.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis()); 
    Cvalues.put(MediaStore.Images.Media.DATA, filepath.toString()); 
    return mresolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Cvalues); 
} 

希望這有助於

+0

問題是獲取文件路徑,因爲我們沒有創建一個文件,我們從來沒有實際上有一個文件路徑,這都是由插入() 處理,除非我已經錯過了幾天可能一直盯着這個問題的東西 –

0

從CommonsWare的建議後,我去了文件保存方法(我知道一世TS不推薦的方法,但它確實有工作的奢侈品)

private void htmlCapture2() { 
    if (!checksd()) { 
     Log.e("sdcard", "failed sd card check"); 
     //we dont have permision so lets not continue 
     return; 
    } 
    if (!checkFolder()) { 
     Log.e("folder", "failed folder check"); 
     //we dont have a folder so lets not continue 
     return; 
    } 
    WebView webView = (WebView) findViewById(R.id.webView); 
    webView.setDrawingCacheEnabled(true); 
    webView.buildDrawingCache(); 
    Bitmap bm = screenshot(webView); 
    String filename = "capture_" + z.getCurrentTimeStamp() + ".png"; 
    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "", filename); 

    Intent share = new Intent(Intent.ACTION_SEND); 
    share.setType("image/png"); 
    //ContentValues values = new ContentValues(); 
    //values.put(MediaStore.Images.Media.TITLE,"title"); 
    //values.put(MediaStore.Images.Media.MIME_TYPE,"image/png"); 
    //Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values); 


    OutputStream outstream; 
    try { 
     file.createNewFile(); 
     FileOutputStream ostream = new FileOutputStream(file); 
     bm.compress(Bitmap.CompressFormat.PNG, 100, ostream); 
     Log.i("ExternalStorage","I think the path is : "+file.toString()); 
     ostream.close(); 
     Log.i("ExternalStorage","I think the path is : "+file.toString()); 
     MediaScannerConnection.scanFile(this, 
       new String[] { file.toString() }, null, 
       new MediaScannerConnection.OnScanCompletedListener() { 
        public void onScanCompleted(String path, Uri uri) { 
         Log.i("ExternalStorage", "Scanned " + path + ":"); 
         Log.i("ExternalStorage", "-> uri=" + uri); 
        } 
       }); 
     //outstream = getContentResolver().openOutputStream(uri); 
     //bm.compress(Bitmap.CompressFormat.PNG,100,outstream); 
     //outstream.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); 
    //share.putExtra(Intent.EXTRA_STREAM,uri); 
    share.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1); 
    startActivity(Intent.createChooser(share,"Share Image")); 
} 

我有些整理上做,當然,圖像不會出現在每比如畫廊,但如果你去在畫廊裏下載相冊,這是對之前的改進。