2013-04-16 61 views
0

這是我的示例代碼,它將圖庫選擇的圖像保存到應用程序內存/data/data/com.isummation.customgallery/files/。但我想將文件路徑更改爲/data/data/com.isummation.customgallery/files/photos。我需要做什麼?我如何改變它?如何更改應用程序的默認存儲空間

package com.isummation.customgallery; 


import com.database.DataBase; 

public class AndroidCustomGalleryActivity extends Activity { 

private int count; 
private Bitmap[] thumbnails; 
private boolean[] thumbnailsselection; 
private String[] arrPath; 
private ImageAdapter imageAdapter; 

Cursor imagecursor; 

int image_column_index; 

Button selectBtn; 

ProgressDialog myProgressDialog = null; 

DataBase db; 
     public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.gallery); 

    showProgress(); 
    new Thread() { 
     public void run() { 
      try 
      { 
       loadFeed(); 
       android.os.Message alertMessage = new android.os.Message(); 
       alertMessage.what = 1; 
       handle.sendMessage(alertMessage); 
      } 
      catch(Exception e) 
      { 
       android.os.Message alertMessage = new android.os.Message(); 
       alertMessage.what = 2; 
       handle.sendMessage(alertMessage);  
      } 
     } 
    }.start(); 


    selectBtn = (Button) findViewById(R.id.selectBtn); 
    selectBtn.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      showProgress(); 
      new Thread() { 
       public void run() { 
        try 
        { 
         SelecedtPhotos(); 
         android.os.Message alertMessage = new 
      android.os.Message(); 
         alertMessage.what = 3; 
         handle.sendMessage(alertMessage); 
        } 
        catch(Exception e) 
        { 
         android.os.Message alertMessage = new 
     android.os.Message(); 
         alertMessage.what = 2; 
         handle.sendMessage(alertMessage);  
        } 
       } 
      }.start(); 


     } 
    }); 

} 

public static byte[] getBitmapAsByteArray(Bitmap bitmap) { 
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
    bitmap.compress(CompressFormat.PNG, 0, outputStream);  
    return outputStream.toByteArray(); 
} 

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

public void loadFeed() 
{ 
    final String[] columns = { MediaStore.Images.Media.DATA, 
     MediaStore.Images.Media._ID }; 
    final String orderBy = MediaStore.Images.Media._ID; 

    imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
     columns, null,null, orderBy); 

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

    imagecursor.close(); 
    } 

private void showProgress() 
    { 
     myProgressDialog = 
ProgressDialog.show(AndroidCustomGalleryActivity.this,null, 
"Loading Data...", true); 
    } 

    private void hideProgress() 
    { 
     if (myProgressDialog != null) 
      myProgressDialog.dismiss(); 
    } 

    ///////////////////// Get File Name from path //////////////////////////// 
public String FileName(String path) 
{ 
    String f = " /"; 
    boolean c = false; 

    for(int i=path.length()-1;i>0;i--) 
    { 
     if(c == false) 
      if(path.charAt(i) == f.charAt(1)) 
      { 
       c = true; 
       return 
    path.substring(i+1,path.length());   
      } 
    } 

    return ""; 
} 

///////////////////// Get Extension from path //////////////////////////// 
    public String fileExt(String audio) 
    { 
    String fileName = ""; 
    String f = " ."; 
    boolean c = false; 

    for(int i=audio.length()-1;i>0;i--) 
    { 
     if(c == false) 

      if(audio.charAt(i) == f.charAt(1)) 
      { 
       fileName = audio.substring(i+1,audio.length()); 
       c = true; 
      } 
    } 

    return fileName; 
    } 

    public void SelecedtPhotos() 
    { 

    final int len = thumbnailsselection.length; 
// int cnt = 0; 

    for (int i =0; i<len; i++) 
    { 
     if (thumbnailsselection[i]) 
     { 
      //cnt++; 

      BitmapFactory.Options options = new 
    BitmapFactory.Options(); 
      options.inSampleSize = 8; 
      options.inPreferredConfig = Bitmap.Config.ARGB_8888; 

      Bitmap bitmap = BitmapFactory.decodeFile(arrPath[i], 
    options); 


      try { 






       FileOutputStream outputStream = 
    openFileOutput(FileName(arrPath[i]), Context.MODE_PRIVATE); 
       outputStream.write(getBitmapAsByteArray(bitmap)); 
       outputStream.close(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      db = new DataBase(getBaseContext()); 
      try { 
       db.createDataBase(); 
      } catch (IOException e1) { 
       e1.printStackTrace(); 
      } 

      db.insert_update("INSERT INTO Photos(name,ext,path) 
    VALUES ('"+FileName(arrPath[i])+"','"+fileExt(arrPath[i])+"','"+arrPath[i]+"')"); 

      db.close(); 

      File file = new File(arrPath[i]); 
      boolean t = file.delete(); 

     } 
    } 

回答

0

相反的:

FileOutputStream outputStream = openFileOutput(FileName(arrPath[i]), Context.MODE_PRIVATE); 

,你需要做更多的事情是這樣的:

File photos=new File(getFilesDir(), "photos"); 
photos.mkdirs(); 
FileOutputStream outputStream=new FileOutputStream(new File(photos, FileName(arrPath[i]))); 
+0

日Thnx是工作 –

相關問題