I am not able to set multiple image in grid view?

这一生的挚爱 提交于 2019-12-25 03:08:29

问题


I am creating app in which 1.take photo from camera. 2.save its URI in database 3.using cursor and adaptor i retrieve image uri and sets in grid view.

i am getting error as " error opening trace file: No such file or directory (2)" also i cant see any folder formed with name "my image"

Main activity

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

please help me to find solution


回答1:


Your adapter needs work. WIthin your getView() method you assume the arguments are a URI. IN fact, the arguments are:

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

The first argument, is the position of the item in the gridview you're about to render. The 2nd is the View - which may be recycled. And the 3rd is the parent of the view. You are trying to take the position, a simple integer, and use that as a URI

What you should be doing within your getView is pulling the image out of your database that corresponds to the "position"th item

See this link here for more information on developing loaders for your adapters



来源:https://stackoverflow.com/questions/23295575/i-am-not-able-to-set-multiple-image-in-grid-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!