0

我正在創建應用程序,其中 1.從相機拍攝照片。 2.將其URI保存在數據庫中 3.使用光標和適配器我檢索圖像uri並在網格視圖中設置。我無法在網格視圖中設置多個圖像?

我得到的錯誤是「錯誤打開跟蹤文件:沒有這樣的文件或目錄(2)」 還我看不到名稱爲「我的圖片」

主要業務形成的任何文件夾

public class MainActivity extends ActionBarActivity { 
    private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100; 
    final int MEDIA_TYPE_IMAGE=2; 
    Button click; 
    ImageView image; 
    Uri fileuri; 
    int camera_capture=100; 


    GridView gridactivity; 
    Gridsource source; 
    customiseadapter adapter; 
    ArrayList<String> List; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fragment_main); 

     source=new Gridsource(this); 
     source.open(); 

     gridactivity=(GridView) findViewById(R.id.grid_view); 

     List=source.getallpath(); 
     adapter=new customiseadapter(getApplicationContext(),List); 
     gridactivity.setAdapter(adapter); 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     int id = item.getItemId(); 
     if (id == R.id.Add_button) { 
      captureimage(); 

     } 
     return super.onOptionsItemSelected(item); 
    } 

    private void captureimage() { 
     if(cameraavail()) 
     { 
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      fileuri = getmediafileuri(MEDIA_TYPE_IMAGE); 
      intent.putExtra(MediaStore.EXTRA_OUTPUT , fileuri); 
      startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); 
     } 
     else 
     { 
      Toast.makeText(getApplicationContext(), "device with not camera support", Toast.LENGTH_SHORT).show(); 
     } 
    } 



    @Override 
    protected void onActivityResult(int resultrequest, int resultcode, Intent data) { 
     // TODO Auto-generated method stub 
     super.onActivityResult(resultrequest, resultcode, data); 

     if (resultrequest==CAMERA_CAPTURE_IMAGE_REQUEST_CODE) { 
      if (resultcode==RESULT_OK) { 

       source.createpicture(fileuri.toString()); 
       ArrayList<String> list1 = source.getallpath(); 

       customiseadapter adapter1= new customiseadapter(getApplicationContext(), list1); 
       gridactivity.setAdapter(adapter1); 




      }else if 
      (resultcode==RESULT_CANCELED) 
      { 
       Toast.makeText(getApplicationContext(), 
         "User cancelled image capture", Toast.LENGTH_SHORT) 
         .show(); 
      } 

     } 
    } 

    private Uri getmediafileuri(int arg) { 
     File mediafile= getmediafile(arg); 

      return Uri.fromFile(mediafile) ; 
     } 
    private File getmediafile(int type){ 
     File mediadir=new File(Environment.getExternalStorageDirectory()+"/myimages"); 
     if(!mediadir.exists()) 
     { 
      mediadir.mkdir(); 
     } 

     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", 
       Locale.getDefault()).format(new Date()); 
     File mediafile; 
     if(type==MEDIA_TYPE_IMAGE) 
     { 
      mediafile= new File(mediadir.getPath() + File.separator + "img_" + timeStamp +".png"); 

     }else 
     { 
      return null; 
     } 
     return mediafile; 
    } 

    private boolean cameraavail() { 
     if(getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) 
     return true; 
     else 
      return false; 

     } 




} 

2.Mysqlitehelper

public class Mysqlitehelper extends SQLiteOpenHelper { 



    public static final String Table_grid ="gridtable"; 
    public static final String column_ID ="ID"; 
    public static final String column_URI ="URI"; 

    public static final String DB_name = "Griddb"; 
    public static final int version =1; 
    public static final String Database_create = "create table "+Table_grid+ " (" 
      + column_ID +" integer primary key autoincrement," 
       + column_URI + " text not null);"; 


    public Mysqlitehelper(Context context) { 
      super(context, DB_name, null, version); 
        } 


    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(Database_create); 

    } 


    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS"+ Table_grid); 
     onCreate(db); 
    } 


} 

3.customiseadapter

public class customiseadapter extends BaseAdapter { 
    Context context; 
    ArrayList<String> list; 

    public customiseadapter(Context context, ArrayList<String> list) { 
     super(); 
     this.context = context; 
     this.list = list; 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return list.size(); 
    } 

    @Override 
    public Object getItem(int arg0) { 
     // TODO Auto-generated method stub 
     return list.get(arg0); 
    } 

    @Override 
    public long getItemId(int arg0) { 
     // TODO Auto-generated method stub 
     return arg0; 
    } 

    @Override 
    public View getView(int arg0, View arg1, ViewGroup arg2) { 
     ImageView image = new ImageView(context); 
     Uri uri = Uri.parse(list.get(arg0)); 
     image.setImageURI(uri); 
     image.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     image.setLayoutParams(new GridView.LayoutParams(70, 70)); 
     return image; 
    } 

} 
  1. Gridsource

    public class Gridsource { 
        Mysqlitehelper mydbhelper; 
        SQLiteDatabase database; 
        String[] column_name={Mysqlitehelper.column_ID,Mysqlitehelper.column_URI}; 
    
    
        public Gridsource(Context context) { 
         mydbhelper= new Mysqlitehelper(context); 
        } 
    public void open() 
        { 
         database=mydbhelper.getWritableDatabase(); 
        } 
    public void close() 
    { 
        mydbhelper.close(); 
    } 
    
    public void createpicture(String path) 
    { 
        ContentValues value= new ContentValues(); 
        value.put(Mysqlitehelper.column_URI, path); 
        database.insert(Mysqlitehelper.Table_grid, null, value);  
    } 
    
    
    public ArrayList<String>getallpath() 
    { 
        ArrayList<String> pathlist = new ArrayList<String>(); 
        Cursor cursor = database.query(Mysqlitehelper.Table_grid, column_name, null, null, null, null, null, null); 
        if(cursor.moveToFirst()) 
        do{ 
         String path = new String(); 
         path=cursor.getString(1); 
    
         pathlist.add(path); 
    
        }while(cursor.moveToNext()); 
        cursor.close(); 
    
    
        return pathlist; 
    } 
    } 
    

請幫我找到解決

回答

0

你的適配器需要的工作。通過你的getView()方法,你可以假定參數是一個URI。實際上,參數是:

public View getView(final int position, View convertView, final ViewGroup parent) { 
... 
} 

第一個參數是項目在您要渲染的gridview中的位置。第二個是視圖 - 可以回收。第三是視圖的父母。您正在試圖採取這樣的立場,一個簡單的整數,並使用它作爲一個URI

您應該在getView內做什麼拉出數據庫中對應的「位置」個項的圖像

有關更多信息,請參見this link here開發裝載機您的適配器

+0

第i正在採取從數組列表字符串,並將其作爲uri.But它不工作 – yogesh