将图像添加到android中的sdcard文件夹

时间:2013-02-04 08:11:14

标签: android android-sdcard

我想开发一个Android应用程序,可以选择多个图像并将它们保存在SD卡的文件夹中。我可以选择多个文件,但是当我想将它们保存在sdcard文件夹中时,只有图像名称。当我试图使用GridView显示那些图像时,我已经理解了它没有图像。这是我的代码.....

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.image_selection);

    Bundle b=getIntent().getExtras();
    directoryName=b.getString("key");

    final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
    final String orderBy = MediaStore.Images.Media._ID;
    Cursor imagecursor = managedQuery(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
            null, orderBy);
    int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
    this.count = imagecursor.getCount();
    this.thumbnails = new Bitmap[this.count];
    this.arrPath = new String[this.count];
    this.thumbnailsselection = new boolean[this.count];
    for (int i = 0; i < this.count; i++) {
        imagecursor.moveToPosition(i);
        int id = imagecursor.getInt(image_column_index);
        int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
        thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
                getApplicationContext().getContentResolver(), id,
                MediaStore.Images.Thumbnails.MICRO_KIND, null);
        arrPath[i]= imagecursor.getString(dataColumnIndex);
    }
    GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
    imageAdapter = new ImageAdapter();
    imagegrid.setAdapter(imageAdapter);
    imagecursor.close();

    final Button selectBtn = (Button) findViewById(R.id.selectBtn);
    selectBtn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            final int len = thumbnailsselection.length;
            int cnt = 0;
            String selectImages = "";
            for (int i =0; i<len; i++)
            {
                if (thumbnailsselection[i])
                {
                    cnt++;
                    selectImages = selectImages + arrPath[i] + "|";
                    String sendString=extractString(arrPath[i]);
                    saveImageToSdCard(sendString,arrPath[i]);
                    Toast.makeText(getApplicationContext(),
                            sendString,
                            Toast.LENGTH_LONG).show();
                }
            }
        }
    });
}

public String extractString(String myString)
{
    String finalString="";
    int index = 0;
    int count=0;
    char newCharacter[]=new char[50];
    char[] charArray = myString.toCharArray();
    int length=myString.length();
    for (int i = 0; i < length; i++)
    {
        char charString=charArray[i];
        if(charString=='/')
        {
            index=i;
        }
    }

    for (int i = index+1; i < length; i++)
    {
        newCharacter[count]=charArray[i];
        count++;
    }

    finalString=String.valueOf(newCharacter);
    return finalString;
}

public void saveImageToSdCard(String imageName, String fullPath)
{
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root +"/"+directoryName);
    String fname = imageName;
    final File file = new File (myDir, fname);
    if (file.exists ())file.delete();

    try 
    {
        final Bitmap myBitmap=BitmapFactory.decodeFile(fullPath);
        new Thread() {
            public void run() 
            {
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                if (myBitmap != null) 
                {
                    myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
                }
                try {
                    if (file.createNewFile()) {
                        //
                    } else {

                        //
                    }

                    FileOutputStream fo;
                    fo = new FileOutputStream(file);
                    fo.write(bytes.toByteArray());

                    fo.flush();
                    fo.close();
                    // result.recycle();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
        FileOutputStream stream=new FileOutputStream(file);

        myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
        stream.flush();
        stream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public ImageAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return count;
    }

    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.galleryitem, null);
            holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
            holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);

            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.checkbox.setId(position);
        holder.imageview.setId(position);
        holder.checkbox.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                CheckBox cb = (CheckBox) v;
                int id = cb.getId();
                if (thumbnailsselection[id]){
                    cb.setChecked(false);
                    thumbnailsselection[id] = false;
                } else {
                    cb.setChecked(true);
                    thumbnailsselection[id] = true;
                }
            }
        });
        holder.imageview.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                int id = v.getId();
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*");
                startActivity(intent);
            }
        });
        holder.imageview.setImageBitmap(thumbnails[position]);
        holder.checkbox.setChecked(thumbnailsselection[position]);
        holder.id = position;
        return convertView;
    }
}
class ViewHolder 
{
    ImageView imageview;
    CheckBox checkbox;
    int id;
}

我该如何解决这个问题。谢谢......

1 个答案:

答案 0 :(得分:0)

似乎有一个类似的,已经回答的问题:

How to get path by MediaStore.Images.Media

解决方案是答案的第一个评论。