2015-10-05 51 views
0

我將一些圖像保存在內部存儲器的文件夾中,並將這些所有保存的圖像顯示在單擊按鈕上。現在我想分享社交網站上的當前開放圖像,如臉譜,Gmail等。我能夠共享文本,但不是圖像。如何在文件夾中共享社交網站上的圖像

代碼保存圖像...圖像訪問

RelativeLayout content = (RelativeLayout) findViewById(R.id.relative); 
     content.setDrawingCacheEnabled(true); 
     Bitmap bitmap = content.getDrawingCache(); 

     File myDir=new File("/sdcard/MyCollection"); 
     myDir.mkdirs(); 
     Random generator = new Random(); 
     int n = 10000; 
     n = generator.nextInt(n); 
     String fname = "Image-"+ n +".jpg"; 
     File file = new File (myDir, fname); 
     if (file.exists()) file.delete(); 
     FileOutputStream outStream; 
     try { 
      outStream = new FileOutputStream(file); 
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
      outStream.flush(); 
      outStream.close(); 
     } 
     catch (FileNotFoundException e) 
     { 
      e.printStackTrace(); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 

碼是.......

的ImageButton sharingButton =新的ImageButton(本); sharingButton.setLayoutParams(new ViewGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT,RadioGroup.LayoutParams.WRAP_CONTENT)); sharingButton.setImageResource(R.drawable.alert);

getFromfolder(); 
    String[] projection = {MediaStore.Images.Thumbnails._ID}; 

    cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, 
      projection, 
      null, 
      null, 
      MediaStore.Images.Thumbnails.IMAGE_ID); 
    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID); 
    GridView sdcardImages = (GridView) findViewById(R.id.gridview); 
    sdcardImages.setAdapter(new ImageAdapter()); 
    sdcardImages.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView parent, View v, int position, long id) { 
      String[] projection = {MediaStore.Images.Media.DATA}; 
      cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
        projection, 
        null, 
        null, 
        null); 
      columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToPosition(position); 
      String imagePath = cursor.getString(columnIndex); 
     } 
    }); 

} 

public void getFromfolder() 
{ 
    File file= new File(android.os.Environment.getExternalStorageDirectory(),"MyCollection"); 

    if (file.isDirectory()) 
    { 
     listFile = file.listFiles(); 
     for (int i = 0; i < listFile.length; i++) 
     { 
      f.add(listFile[i].getAbsolutePath()); 
     } 
    } 
} 

public class ImageAdapter extends BaseAdapter { 
    private LayoutInflater mInflater; 

    public ImageAdapter() { 
     mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 
    public int getCount() { 
     return f.size(); 
    } 
    public Object getItem(int position) { 
     return position; 
    } 
    public long getItemId(int position) { 
     return position; 
    } 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if (convertView == null) { 
      holder = new ViewHolder(); 
      convertView = mInflater.inflate(
        R.layout.gelleryitem, null); 
      holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage); 
      convertView.setTag(holder); 
     } 
     else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position)); 
     holder.imageview.setImageBitmap(myBitmap); 
     return convertView; 
    } 
} 
class ViewHolder { 
    ImageView imageview; 
} 

回答

0

首先,你必須將圖像作爲位圖取從文件路徑。然後,您可以通過意圖將圖像傳遞給其他應用程序。如果你想要一個字符串與圖像一起傳遞,你可以像EXTRA_TEXT那樣通過intent傳遞它。在權限中添加WRITE_EXTERNAL_STORAGE。試試這個:

File imgFile = new File(imagePath); 
if(imgFile.exists()){ 
     Bitmap icon = BitmapFactory.decodeFile(imagePath); 
     Intent share = new Intent(Intent.ACTION_SEND); 
     share.setType("image/jpeg"); 

     ContentValues values = new ContentValues(); 
     values.put(Images.Media.TITLE, "title"); 
     values.put(Images.Media.MIME_TYPE, "image/jpeg"); 
     Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, 
     values); 


     OutputStream outstream; 
     try { 
     outstream = getContentResolver().openOutputStream(uri); 
     icon.compress(Bitmap.CompressFormat.JPEG, 100, outstream); 
     outstream.close(); 
     } catch (Exception e) { 
     System.err.println(e.toString()); 
     } 

     //text 
     share.putExtra(Intent.EXTRA_TEXT, "Text that has to be shared"); 
     //image 
     share.putExtra(Intent.EXTRA_STREAM, uri); 
     startActivity(Intent.createChooser(share, "Share Image")); 
} 
+0

分享。 http://stackoverflow.com/questions/22533773/android-how-to-share-image-with-text-on-facebook-via-intent/25065367#25065367 – geokavel

相關問題