2017-03-18 14 views
0

私はJsonから取った画像を共有するつもりです。いくつかの試みの後、私は最終的にこのコードで結論づけました。それは私がwhatsappを開き、私が共有したい連絡先を選択するまでうまく動作し、そこから画像が読み込まれず、投稿をクリックすると、共有に失敗したと表示されます。私は、コメントを削除する場合共有画像URLから

FileOutputStream fOut = new FileOutputStream(file); は、エラーメッセージが表示されますし、私は、キャッチ/試す追加する必要があり

Drawable drawable1 = imageViewPreview.getDrawable(); 
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.id.image_preview); 
File file = new File(SlideshowDialogFragment.this.getCacheDir(), String.valueOf(largeIcon + ".png")); 

// FileOutputStream fOut = new FileOutputStream(file); 
// largeIcon.compress(Bitmap.CompressFormat.JPEG, 100, fOut); 

file.setReadable(true, false); 
final Intent intent = new Intent(Intent.ACTION_SEND); 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); 
intent.setType("image/png"); 
startActivity(intent); 

は、Nullpoint例外に

答えて

0

を言っアプリのクラッシュは、このコードを試してみてください。

Bitmap bitmap = mBitmap; 
Intent share = new Intent(Intent.ACTION_SEND); 
share.setType("image/jpeg"); 
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "photo.jpg"); 
try { 
    f.createNewFile(); 
    FileOutputStream fo = new FileOutputStream(f); 
    fo.write(bytes.toByteArray()); 
} catch (IOException e) {      
     e.printStackTrace(); 
} 
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/photo.jpg")); 
startActivity(Intent.createChooser(share, "Share Image")); 
+0

b.compress(Bitmap.CompressFormat.JPEG、100、bytes); ' – almiters

+0

ちょうど答えを変更しました。更新されたコードを一度試して –

関連する問題