2013-02-04 145 views
0

我想開發一個Android應用程序,可以選擇多個圖像並將它們保存在SD卡的文件夾中。我可以選擇多個文件,但是當我想將它們保存在SD卡文件夾中時,只有圖像名稱。我已經理解了,當我試圖用GridView顯示那些圖像時,沒有圖像。這裏是我的代碼.....將圖像添加到Android的SD卡文件夾

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; 
} 

我怎麼能解決problem.Thanks ...

+0

您確定設備沒有安裝嗎?當使用真實設備並且安裝時,您將無法將圖像保存到SD卡上。如果你正在使用模擬器,我也有保存圖像的問題。使用裝有SD卡的真實設備卸載。 –

+0

我正在使用模擬器 – andDev

+0

嘗試一個真實的設備。我敢打賭,模擬器是這裏的罪魁禍首。你是否在模擬器中加入了SD卡支持並給了它足夠的存儲空間? –

回答

相關問題