2017-06-24 10 views
0

私のアプリでは、webviewのテキストを画像ファイルとして保存し、共有機能を開始してユーザーがファイルを共有できるようにし、画像も画像フォルダに保存します。私は彼らがファイルのブラウザを介して写真のフォルダに表示されますが、ギャラリーは更新されていないことを確認することができます。ギャラリー(および関連付けられた画像ビューア)は、新しい画像の存在をどのように認識するようにしますか?

私は画像を撮影するために使用されるコードは、それははるかにこれを取得するために3日かかった、さらに最新の問題の解決策を探して

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カードをスキャンすると言うのソリューションを見てきましたが、これは悪いアイデアだと私は絶望からそれらをしようとしたとき、彼らはすべての支援を大幅に

+0

私は決して 'mediaStore'がここで何をしようとしているかについて特に詳しくは書かれていないので、これには' insert() 'を使っていません。イメージをファイルに書き込んだ後、 'MediaScannerConnection'レシピに従うことをお勧めします。 – CommonsWare

+0

私はこの方法でメディアストアを使用することが推奨されたルートだと言われましたが、ファイルルートに移動して画像を保存することで修正しました。ありがとうございます。 –

答えて

0

を高く評価し

最新のSDK

で動作していないようですあなた新しい画像が追加されたことをアンドロイドメディアストアを通知する必要があり、以下の

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); 
} 

希望により、これがCommonsWareからの提案の後

+0

問題はファイルパスを取得しています。私たちはファイルパスを実際には持っていないので、insert() によって処理されたものは、この問題を数日間見つめていない限り、 –

0

を助けること行うことができ、私は知っている(ファイルの保存方法に行ってきました私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")); 
} 

は、私はいくつかもちろんやってそれを片付けている、とあなたが行く場合ものの画像は、当たりと言うのギャラリーには表示されません。ギャラリーにアルバムをダウンロードするには前に改良があります。

関連する問題