0

嗨,我正在做一个应用程序,在这里我需要从图库文件夹或相机中选择更多图像。然后我需要将这些选定的图像保存到 sdcard 其他文件夹中。使用下面的代码我可以从图库中选择图像,然后存储选定的图像一个字符串数组列表中的图像,但是如何将这些选定的图像存储到 sdcard...

  public class MultiPhotoSelectActivity extends BaseActivity {
private Button dialogButton1,dialogButton2,dialogButton3,cancel;
Dialog dialog;
String extStorageDirectory;

Bitmap bm;
private ArrayList<String> imageUrls;
private DisplayImageOptions options;
private ImageAdapter imageAdapter;
final Context context = this;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ac_image_grid);

    //Bundle bundle = getIntent().getExtras();
    //imageUrls = bundle.getStringArray(Extra.IMAGES);

    final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
    final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
    Cursor imagecursor = managedQuery(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
            null, orderBy + " DESC");

    this.imageUrls = new ArrayList<String>();

    for (int i = 0; i < imagecursor.getCount(); i++) {
        imagecursor.moveToPosition(i);
        int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
        imageUrls.add(imagecursor.getString(dataColumnIndex));

        System.out.println("=====> Array path => "+imageUrls.get(i));
    }


    options = new DisplayImageOptions.Builder()
        .showStubImage(R.drawable.stub_image)
        .showImageForEmptyUri(R.drawable.image_for_empty_url)
        .cacheInMemory()
        .cacheOnDisc()
        .build();

    imageAdapter = new ImageAdapter(this, imageUrls);

    GridView gridView = (GridView) findViewById(R.id.gridview);
    gridView.setAdapter(imageAdapter);
    /*gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            startImageGalleryActivity(position);
        }
    });*/
}

@Override
protected void onStop() {
    imageLoader.stop();
    super.onStop();
}


public void btnChoosePhotosClick(View v){

ArrayList<String> selectedItems = imageAdapter.getCheckedItems();
        }

public class ImageAdapter extends BaseAdapter {

    ArrayList<String> mList;
    LayoutInflater mInflater;
    Context mContext;
    SparseBooleanArray mSparseBooleanArray;

    public ImageAdapter(Context context, ArrayList<String> imageList) {
        // TODO Auto-generated constructor stub
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mSparseBooleanArray = new SparseBooleanArray();
        mList = new ArrayList<String>();
        this.mList = imageList;

    }

    public ArrayList<String> getCheckedItems() {
        ArrayList<String> mTempArry = new ArrayList<String>();

        for(int i=0;i<mList.size();i++) {
            if(mSparseBooleanArray.get(i)) {
                mTempArry.add(mList.get(i));
            }
        }

        return mTempArry;
    }

    @Override
    public int getCount() {
        return imageUrls.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null) {
            convertView = mInflater.inflate(R.layout.row_multiphoto_item, null);
        }

        CheckBox mCheckBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
        final ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);

        imageLoader.displayImage("file://"+imageUrls.get(position), imageView, options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(Bitmap loadedImage) {
                Animation anim = AnimationUtils.loadAnimation(MultiPhotoSelectActivity.this, R.anim.fade_in);
                imageView.setAnimation(anim);
                anim.start();
            }
        });

        mCheckBox.setTag(position);
        mCheckBox.setChecked(mSparseBooleanArray.get(position));
        mCheckBox.setOnCheckedChangeListener(mCheckedChangeListener);

        return convertView;
    }

    OnCheckedChangeListener mCheckedChangeListener = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
        }
    };
}
} 
4

1 回答 1

0
private void saveDataInSdCard(String urimg) {
 URL url = new URL(urimg);

    OutputStream fOut = null;
    Uri outputFileUri;
    try {
        File root = new File(Environment.getExternalStorageDirectory()
                + File.separator + "urFlodername" + File.separator);
        root.mkdirs();
        sdImageMainDirectory = new File(root,i+"myPicName.jpg");
        outputFileUri = Uri.fromFile(sdImageMainDirectory);
        fOut = new FileOutputStream(sdImageMainDirectory);
    } catch (Exception e) {

    }

    Bitmap bm=BitmapFactory.decodeStream(url.openConnection().getInputStream());
    try {
        bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
    } catch (Exception e) {
    }       

}

然后在图像列表的 for 循环中调用此方法。

For(int i = 0; i < imagesUrls.size(); i++){
        saveDataInSdCard(imagesUrls.get(i)); }
于 2013-06-07T12:12:51.090 回答