android-从SD卡共享图像

android-从SD卡共享图像,第1张

概述我尝试了2周,以查找如何成功共享存储在SD卡中的图像.这个answer对我不起作用,也不是我要找的东西.我正在使用CamPreview应用程序将图像存储到SD,然后在应用程序内库中显示它们:publicclassGalleryViewextendsActivity{privatestaticfinalintSELECT_PICTURE

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

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

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

   public class galleryVIEw extends Activity {     private static final int SELECT_PICTURE = 1;private String selectedImagePath;@OverrIDepublic 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);}@OverrIDepublic 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;}

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

总结

以上是内存溢出为你收集整理的android-从SD卡共享图像全部内容,希望文章能够帮你解决android-从SD卡共享图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1085212.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存