ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android-从SD卡共享图像

2019-11-01 08:24:35  阅读:223  来源: 互联网

标签:image share android-sdcard android


我尝试了2周,以查找如何成功共享存储在SD卡中的图像.

这个answer对我不起作用,也不是我要找的东西.

我正在使用Cam Preview应用程序将图像存储到SD,然后在应用程序内库中显示它们:

   public class GalleryView extends Activity {  

   private static final int SELECT_PICTURE = 1;

private String selectedImagePath;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallery_view);

    Gallery g = (Gallery) findViewById(R.id.gallery);
    g.setAdapter(new ImageAdapter(this, ReadSDCard()));


    g.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
        }
    });
}

  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);

            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, selectedImagePath);
            shareIntent.setType("image/*");
            startActivity(Intent.createChooser(shareIntent, "Share image"));
        }
    }
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

@Override
public void onDestroy() {
    super.onDestroy();       
}

private List<String> ReadSDCard() {

    createDirIfNotExists();

    List<String> tFileList = new ArrayList<String>();

    //It have to be matched with the directory in SDCard
    File f = new File("/sdcard/Cam App");

    File[] files=f.listFiles();

    for(int i=0; i<files.length; i++) {
        File file = files[i];
        /*It's assumed that all file in the path are in supported type*/
        tFileList.add(file.getPath());
    }

    return tFileList;
}

class ViewHolder {
    ImageView i;
}

public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;
    private List<String> FileList;
    LayoutInflater inflater = getLayoutInflater();
    ViewHolder holder;

    public ImageAdapter(Context c, List<String> fList) {
        mContext = c;
        FileList = fList;
        TypedArray a = obtainStyledAttributes(R.styleable.GalleryTheme);
        mGalleryItemBackground = a.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
        a.recycle();
    }

    public int getCount() {
        return FileList.size();
    }

    public Object getItem(int position) {
        return position;
    } 

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);

        Bitmap bm = BitmapFactory.decodeFile(FileList.get(position).toString());
        i.setImageBitmap(bm);

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.gallery_item, parent, false);

            holder = new ViewHolder();
            holder.i = ((ImageView) convertView.findViewById(R.id.imagenGallery));

            convertView.setTag(holder);
        }
        holder.i.setScaleType(ImageView.ScaleType.FIT_XY);
        holder.i.setBackgroundResource(mGalleryItemBackground);

        return i;
    }
}

public TypedArray obtainStyledAttributes(int theme) {
    // TODO Auto-generated method stub
    return null;
}
}

画廊工作正常,但我不知道如何共享保存在SD上的图像.

如何访问Cam App文件夹中的图像并共享那一刻出现在图库中的图像?

我尝试使用位置,但是它仅适用于恒定图像.任何帮助/提示将不胜感激.

解决方法:

好吧,我终于找到了解决方案,但这并不是我想要的:

g.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.setDataAndType(Uri.parse("file://" + "/sdcard/Cam App"), "image/*");
            startActivityForResult(Intent.createChooser(intent, "Select image to share:"), SELECT_PICTURE);
        }
    });
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            //OI FILE Manager
            filemanagerstring = selectedImageUri.getPath();

            //MEDIA GALLERY
            selectedImagePath = getPath(selectedImageUri);

            //NOW WE HAVE OUR WANTED STRING
            if(selectedImagePath!=null)
                System.out.println("selectedImagePath is the right one for you!");
            else
                System.out.println("filemanagerstring is the right one for you!");

            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, selectedImageUri);
            shareIntent.setType("image/*");
            startActivity(Intent.createChooser(shareIntent, "Share image via:"));
        }
    }
}

@SuppressWarnings("deprecation")
public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    if(cursor!=null) {
        //HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
        //THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    else return null;
}

如果有人有更好的解决方案,请分享.

标签:image,share,android-sdcard,android
来源: https://codeday.me/bug/20191101/1982399.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有