2013-08-29 41 views
7

Tôi đang tích hợp chia sẻ với mã sau cho ứng dụng.Chia sẻ hình ảnh png trong thư mục drawable

private void socialShare() 
    { 
     Uri uri = Uri.parse("android.resource://com.example.myproject/drawable/appicon"); 
     Intent shareIntent = new Intent(); 
     shareIntent.setAction(Intent.ACTION_SEND); 
     shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 
     shareIntent.putExtra(Intent.EXTRA_TEXT, "sharing myapp"); 
     shareIntent.setType("image/jpeg"); 
     startActivity(Intent.createChooser(shareIntent, "Share from")); 
    } 

Như trong đoạn mã trên, tôi đang cố gắng đặt png image trong thư mục có thể kéo. Nhưng hình ảnh không thể gửi được. Có phải vì trong setType, đó là image/jpeg? Tôi không thể sử dụng jpeg, bởi vì nó mất tính minh bạch. Có thể một số người đề nghị tôi chia sẻ hình ảnh không?

Đây là mã mà tôi sử dụng để sao chép các hình ảnh từ drawable để sdcard:

String commonPath = Environment.getExternalStorageDirectory().toString() + "/MyAppFolder"; 
     File direct = new File(commonPath); 

     if(!direct.exists()) 
     { 
      if(direct.mkdir()) 
       { 
       Log.d("tag","directory created"); 
       //directory is created; 
       } 

     } 

     Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.sharingimage); 
     OutputStream outStream = null; 
      File savingFile = new File(commonPath, "shareImage.png"); 
      if(!savingFile.exists()) 
      { 
       Log.d("tag","file is created"); 

      try { 
       savingFile.createNewFile(); 
       outStream = new FileOutputStream(savingFile); 
       bm.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
       outStream.flush(); 
       outStream.close(); 

       Log.d("tag","Saved"); 

       } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 

       } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 

       } 

      } 

Trả lời

1

Bạn không thể trực tiếp chia sẻ một uri từ bạn ứng dụng lưu trữ nội bộ (tất nhiên các resourses của ứng dụng của bạn sẽ luôn luôn trong bộ nhớ trong)

có hai cách để đạt được điều này ..

  1. Sao chép hình ảnh của bạn để lưu trữ bên ngoài sau đó chia sẻ nó từ đó . Xem this

  2. Viết nhà cung cấp nội dung để chia sẻ hình ảnh. Cho rằng tham khảo Create and Share a File from Internal Storage

+0

Cảm ơn câu trả lời của bạn. Nhưng tôi không thể làm theo cách thứ nhất. – rick

+0

bạn đã sao chép hình ảnh vào bộ nhớ ngoài –

+0

Không. Tôi không thể sao chép hình ảnh. Nó luôn luôn ném tập tin không tìm thấy ngoại lệ. Bạn có thể vui lòng hiển thị cho tôi đoạn mã nếu bạn đã có đoạn mã đó không? – rick

10

Bạn có thể chia sẻ bằng cách sử dụng phương pháp sau đây ...

Intent shareIntent = new Intent(); 
shareIntent.setAction(Intent.ACTION_SEND); 
shareIntent.setType("image/png"); 
Uri uri = Uri.parse("android.resource://your package name/"+R.drawable.ic_launcher); 
shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello, This is test Sharing"); 
startActivity(Intent.createChooser(shareIntent, "Send your image")); 

Cố gắng và thử nghiệm ... làm việc cho tôi

+0

Không, nó không hoạt động. Nó nói rằng tất cả các mục không thể được đính kèm. Tôi đã thử nghiệm cho fb, google +, gmail, ứng dụng nhắn tin. Không có gì hiệu quả. – rick

+0

tôi đã thử, nó không hoạt động –

+0

không làm việc cho tôi hoặc – jonney

12

tôi tìm thấy giải pháp mà tôi không nên sao chép hình ảnh trên thẻ sd. Ở đây là:

try { 
     Uri imageUri = null; 
     try { 
      imageUri = Uri.parse(MediaStore.Images.Media.insertImage(this.getContentResolver(), 
        BitmapFactory.decodeResource(getResources(), R.drawable.fragment_menu_logo), null, null)); 
     } catch (NullPointerException e) { 
     } 
     String text = getString(R.string.share_text); 
     // Launch the Google+ share dialog with attribution to your app. 
     Intent shareIntent = new PlusShare.Builder(mActivity) 
       .setType("image/*") 
       .setText(text) 
       .addStream(imageUri) 
       .getIntent(); 
     startActivity(shareIntent); 
    } catch (android.content.ActivityNotFoundException ex) { 
     Toast.makeText(mActivity, mActivity.getString(R.string.share_google_plus_not_installed), Toast.LENGTH_LONG).show(); 
    } 
+0

Điều này làm việc trên các giải pháp trước đó, cảm ơn! –

+1

@RSenApps Tôi quên thêm NullPointerException đánh bắt (Ở đây http://stackoverflow.com/questions/12230942/why-images-media-insertimage-return-null giải thích khi nào có thể). Xem cập nhật câu trả lời. –

+0

Điều này làm cho bản vẽ mất đi sự trong suốt của nó, bạn có tìm cách xử lý điều đó không? –

7
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.xxxx); 
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/LatestShare.jpg"; 
OutputStream out = null; 
File file=new File(path); 
try { 
    out = new FileOutputStream(file); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); 
    out.flush(); 
    out.close(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
path=file.getPath(); 
Uri bmpUri = Uri.parse("file://"+path); 
Intent shareIntent = new Intent(); 
shareIntent = new Intent(android.content.Intent.ACTION_SEND); 
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri); 
shareIntent.setType("image/jpg"); 
startActivity(Intent.createChooser(shareIntent,"Share with")); 

này làm việc cho tôi một cách hoàn hảo, và điều này sẽ cần quyền ghi

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

Thêm dòng này để android manifest cho phép ghi.

+0

Hoạt động như một sự quyến rũ. Cảm ơn người đàn ông, bạn đã cứu ngày của tôi :) – Ale

+0

Tuyệt vời .... !!! câu trả lời đơn giản và ngọt ngào .. !!! – Nils

3

add Đầu phép

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

sử dụng Bitmap từ res

Bitmap b =BitmapFactory.decodeResource(getResources(),R.drawable.userimage); 
      Intent share = new Intent(Intent.ACTION_SEND); 
      share.setType("image/jpeg"); 
      ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
      b.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
      String path = MediaStore.Images.Media.insertImage(getContentResolver(), 
        b, "Title", null); 
      Uri imageUri = Uri.parse(path); 
      share.putExtra(Intent.EXTRA_STREAM, imageUri); 
      startActivity(Intent.createChooser(share, "Select")); 

Tested qua bluetooth.

Hoạt động như một Hình tượng.

Các vấn đề liên quan