如何在文件夹

时间:2015-10-05 05:10:03

标签: android

我将一些图像保存在内部存储器的文件夹中,并在按钮单击时显示所有保存的图像。现在我想在Facebook,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 = new ImageButton(this);         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;
}

2 个答案:

答案 0 :(得分:0)

要将图像添加为电子邮件附件,请在此处查看Intent.EXTRA_STREAM部分:https://developer.android.com/guide/components/intents-common.html#Email

对于Facebook,请查看此处:https://developers.facebook.com/docs/sharing/android

答案 1 :(得分: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"));
}