2013-04-16 71 views
0

我的代碼是拯救畫廊圖像在默認位置/數據/數據/的applicationName /文件如何更改默認的文件路徑

,如果我在AppPhotos類更改路徑「/data/data/com.isummation.customgallery /文件/「; 這是不是在列表中顯示任何圖像什麼是艾杜? PLZ幫我

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; 

    Handler handle = new Handler(){   
    public void handleMessage(Android.os.Message message) { 

    if (message.what == 1) 
    { 
     hideProgress(); 

     GridView imagegrid = (GridView) 
         findViewById(R.id.PhoneImageGrid); 
     imageAdapter = new ImageAdapter(); 
     imagegrid.setAdapter(imageAdapter); 
    } 
    else if (message.what == 3) 
    { 
     hideProgress(); 

     AndroidCustomGalleryActivity.this.finish(); 

    } 
    else if (message.what == 2) 
    { 
     hideProgress(); 
    } 
    super.handleMessage(msg); 
}; 
}; 

    /** Called when the activity is first created. */ 
    @Override 
    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_KI ND, 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.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(); 

    } 
    } 
    } 
    } 









     import android.view.View; 
     import android.widget.AdapterView; 
     import android.widget.AdapterView.OnItemClickListener; 
     import android.widget.ListView; 

      import com.database.DataBase; 
      import com.isummation.customgallery.R; 

        public class AppPhotos extends Activity{ 

     ProgressDialog myProgressDialog = null; 

    ListView list; 

    Activity activity = AppPhotos.this; 

List<AppPhotosData> picList; 

    DataBase db; 

    int position; 

    Handler handle = new Handler(){ 
    public void handleMessage(Android.os.Message message) { 

    if (message.what == 1) 
    { 
     hideProgress(); 

     list.setAdapter(new 
      AppPhotosAdapter(getApplicationContext(),activity,0,picList)); 
    } 
    else if (message.what == 2) 
    { 
     hideProgress(); 
    } 
    super.handleMessage(msg); 
}; 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.photolist); 

    list = (ListView) findViewById(R.id.listView1); 
    list.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> arg0, View arg1, 
      int arg2, long arg3) { 

     position = arg2; 

     AlertDialog.Builder builder = new 
       AlertDialog.Builder(AppPhotos.this); 
     builder.setTitle("Hidden Photos"); 
     builder.setMessage("Are you sure you want to revert the 
     photo?"); 
     builder.setPositiveButton("Yes", new 
     DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel(); 

        DialogBtn(); 
       } 
     }); 
     builder.setNegativeButton("No", new 
      DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel();   
       } 
     }); 
     AlertDialog dialog = builder.create(); 
     dialog.show(); 

    } 
    }); 

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

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

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

public void loadFeed() 
{ 
String filePaths = "/data/data/com.isummation.customgallery/files/"; 
File outputFile1 = new File(filePaths); 
File[] files1 = outputFile1.listFiles(); 

picList = new ArrayList<AppPhotosData>(); 

for(File f:files1) 
{ 
    AppPhotosData picObj = new AppPhotosData(); 

    Bitmap bmDulicated4 = BitmapFactory.decodeFile(f.getAbsolutePath());; 

    picObj.setImage(bmDulicated4); 
    picObj.setName(f.getName()); 

    picList.add(picObj); 
} 
} 

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

    e1.printStackTrace(); 
} 

Cursor DataC = db.selectQuery("SELECT path FROM Photos where name 
    ='"+picList.get(position).getName()+"'"); 

if(DataC.getCount() > 0) 
{ 
    Bitmap bitmap = picList.get(position).getImage(); 

    if(picList.get(position).getImage() != null) 
    { 
     try { 
      FileOutputStream outputStream = new 
     FileOutputStream(DataC.getString(DataC.getColumnIndex("path"))); 
      outputStream.write(getBitmapAsByteArray(bitmap)); 
      outputStream.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     File file = new File("/data/data/com.isummation.customgallery 
    /files/"+picList.get(position).getName()); 
     file.delete(); 

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

    db.close(); 
} 

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

您是否創建了'com.isummation.customgallery/files /'文件夾?爲什麼不嘗試類似'customgallery/files /'來查看點是否不會導致這種迴歸? – g00dy

+0

我想將圖像保存在我的應用程序內部存儲器中 – nazeerahmed

+0

您不應該在應用程序中對路徑進行硬編碼,因爲可能某些其他手機沒有相同的文件系統。相反,檢查此:http://developer.android.com/guide/topics/data/data-data-storage.html#filesInternal – Aballano

回答

0

手動在SD卡中創建你的應用程序目錄,然後將圖像保存在該目錄中。 你可以嘗試下面的代碼片段。

    String root=Environment.getExternalStorageDirectory().toString(); 
       File myDir = new File(root+"/demo_image"); 
       if(!myDir.exists()) 
       { 
        myDir.mkdir(); 
       } 

       String fname = "Image"+String.valueOf(System.currentTimeMillis())+".jpg"; 
       File file = new File (myDir, fname); 
       if (file.exists()) file.delete(); 
       try { 
         FileOutputStream out = new FileOutputStream(file); 
         photo.compress(Bitmap.CompressFormat.JPEG,100, out); 
         out.flush(); 
         out.close(); 

       } catch (Exception e) { 
         e.printStackTrace(); 
       } 
+0

不幫我,我想克里特應用程序內的目錄不在SD卡 – nazeerahmed

+0

這就是'myDir.mkdir ();'是爲;-) – g00dy