Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从SD卡共享图像_Android_Image_Share_Android Sdcard - Fatal编程技术网

Android 从SD卡共享图像

Android 从SD卡共享图像,android,image,share,android-sdcard,Android,Image,Share,Android Sdcard,我花了两周的时间试图找到如何共享存储在SD卡上的图像,但没有成功 这对我不起作用,也不是我想要的 我正在使用Cam预览应用程序,该应用程序将图像存储到SD,然后在应用程序库中显示它们: public class GalleryView extends Activity { private static final int SELECT_PICTURE = 1; private String selectedImagePath; @Override public void o

我花了两周的时间试图找到如何共享存储在SD卡上的图像,但没有成功

这对我不起作用,也不是我想要的

我正在使用Cam预览应用程序,该应用程序将图像存储到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;
}
}
公共类GalleryView扩展活动{
私有静态最终整数选择_PICTURE=1;
私有字符串selectedImagePath;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_视图);
画廊g=(画廊)findViewById(R.id.Gallery);
g、 setAdapter(新的ImageAdapter(此为ReadsCard());
g、 setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父视图、视图v、整型位置、长id){
意图=新意图();
intent.setType(“image/*”);
intent.setAction(intent.ACTION\u GET\u CONTENT);
startActivityForResult(Intent.createChooser(Intent,“选择图片”),选择图片);
}
});
}
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
if(resultCode==RESULT\u OK){
if(requestCode==选择图片){
Uri selectedImageUri=data.getData();
selectedImagePath=getPath(selectedImageUri);
意向共享意向=新意向();
shareIntent.setAction(Intent.ACTION\u SEND);
shareIntent.putExtra(Intent.EXTRA\u流,选择图像路径);
setType(“image/*”);
startActivity(Intent.createChooser(shareIntent,“共享图像”));
}
}
}
公共字符串getPath(Uri){
字符串[]投影={MediaStore.Images.Media.DATA};
Cursor Cursor=managedQuery(uri、投影、null、null、null);
int column_index=游标
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
返回cursor.getString(列索引);
}
@凌驾
公共空间{
super.ondestory();
}
私人列表读卡器(){
createDirIfNotExists();
List-tFileList=new-ArrayList();
//它必须与SD卡中的目录匹配
文件f=新文件(“/sdcard/Cam App”);
File[]files=f.listFiles();

对于(int i=0;i嗯,我终于找到了一个解决方案,但这并不是我想要的:

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;
}
g.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父视图、视图v、整型位置、长id){
意图=新意图();
intent.setAction(intent.ACTION\u GET\u CONTENT);
setDataAndType(Uri.parse(“文件://”+“/sdcard/Cam-App”),“image/*”;
startActivityForResult(Intent.createChooser(Intent,“选择要共享的图像:”),选择图片);
}
});
}
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
if(resultCode==RESULT\u OK){
if(requestCode==选择图片){
Uri selectedImageUri=data.getData();
//文件管理器
filemanagerstring=selectedImageUri.getPath();
//媒体画廊
selectedImagePath=getPath(selectedImageUri);
//现在我们有了通缉犯
如果(selectedImagePath!=null)
System.out.println(“selectedImagePath是适合您的!”;
其他的
println(“filemanagerstring是适合您的!”;
意向共享意向=新意向();
shareIntent.setAction(Intent.ACTION\u SEND);
shareIntent.putExtra(Intent.EXTRA_流,选择edimageri);
setType(“image/*”);
startActivity(Intent.createChooser(shareIntent,“通过:”)共享图像);
}
}
}
@抑制警告(“弃用”)
公共字符串getPath(Uri){
字符串[]投影={MediaStore.Images.Media.DATA};
Cursor Cursor=managedQuery(uri、投影、null、null、null);
如果(光标!=null){
//在这里,如果游标为NULL,您将得到一个NULL指针
//如果使用OI文件管理器拾取介质,则可能会出现这种情况
int column_index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
返回cursor.getString(列索引);
}
否则返回null;
}

如果有人有更好的解决方案,请与我们分享。

如果您知道该图像的路径,下面的代码可以正常工作。

Intent shareit=new Intent(Intent.ACTION_SEND);
shareit.setType("image/jpeg");
File f = new File("imagepath");
shareit.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
startActivity(Intent.createChooser(shareit, "Share Image Via"));
创建具有图像文件名的文件


File f=new File(Environment.getExternalStorageDirectory()+File.separator+“image_File.jpg”);

05-30 18:51:11.515:E/AndroidRuntime(16500):java.lang.RuntimeException:未能将结果ResultInfo{who=null,request=1,result=-1,data=Intent{(有额外内容)}传递到活动{com.nightinart.camapp/com.nightinart.camapp.GalleryView}:java.lang.NullPointerException。这是我的最后一个日志:(我现在已经尝试过了:public void onActivityResult(int-requestCode,int-resultCode,Intent-data){if(resultCode==RESULT\u-OK){if(requestCode==SELECT\u-PICTURE){Uri selectedImageUri=data.getData();selectedImagePath=getPath(selectedImageUri);