我不能够在网格视图设置多个图像? [英] I am not able to set multiple image in grid view?
问题描述
我正在创建中的应用 1.take的照片从相机。 2.save其URI数据库 3.使用鼠标和适配器我检索图像URI和套网格视图。
我收到错误的错误打开曲线文件:没有这样的文件或目录(2) 此外,我看不到有名为我的图片
形成的任何文件夹主要活动
公共类MainActivity扩展ActionBarActivity {
私有静态最终诠释CAMERA_CAPTURE_IMAGE_REQUEST_ code = 100;
最终诠释MEDIA_TYPE_IMAGE = 2;
单击按钮;
ImageView的形象;
乌里了fileURI;
INT camera_capture = 100;
GridView控件gridactivity;
Gridsource来源;
customiseadapter适配器;
ArrayList的<字符串>清单;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.fragment_main);
来源=新Gridsource(本);
source.open();
gridactivity =(GridView控件)findViewById(R.id.grid_view);
名单= source.getallpath();
适配器=新customiseadapter(getApplicationContext(),清单);
gridactivity.setAdapter(适配器);
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
INT的id = item.getItemId();
如果(ID == R.id.Add_button){
captureimage();
}
返回super.onOptionsItemSelected(项目);
}
私人无效captureimage(){
如果(cameraavail())
{
意向意图=新的意图(MediaStore.ACTION_IMAGE_CAPTURE);
=了fileURI getmediafileuri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,了fileURI);
startActivityForResult(意向,CAMERA_CAPTURE_IMAGE_REQUEST_ code);
}
其他
{
Toast.makeText(getApplicationContext(),设备有没有摄像头的支持,Toast.LENGTH_SHORT).show();
}
}
@覆盖
保护无效onActivityResult(INT resultrequest,诠释结果code,意图数据){
// TODO自动生成方法存根
super.onActivityResult(resultrequest,因此code,数据);
如果(resultrequest == CAMERA_CAPTURE_IMAGE_REQUEST_ code){
如果(结果code == RESULT_OK){
source.createpicture(fileuri.toString());
ArrayList的<字符串>的List1 = source.getallpath();
customiseadapter适配器1 =新customiseadapter(getApplicationContext(),List1中);
gridactivity.setAdapter(适配器1);
}否则,如果
(结果code == RESULT_CANCELED)
{
Toast.makeText(getApplicationContext(),
用户已取消图像捕捉,Toast.LENGTH_SHORT)
。显示();
}
}
}
私人乌里getmediafileuri(INT ARG){
文件媒体文件= getmediafile(ARG);
返回Uri.fromFile(媒体文件);
}
私人文件getmediafile(整型){
文件mediadir =新的文件(Environment.getExternalStorageDirectory()+/ myimages);
如果(!mediadir.exists())
{
mediadir.mkdir();
}
字符串的timeStamp =新的SimpleDateFormat(YYYYMMDD_HHMMSS
。Locale.getDefault())格式(新的日期());
文件媒体文件;
如果(类型== MEDIA_TYPE_IMAGE)
{
媒体文件=新的文件(mediadir.getPath()+文件分割符+IMG_+的timeStamp +PNG);
}其他
{
返回null;
}
返回媒体文件;
}
私人布尔cameraavail(){
如果(getApplicationContext()。getPackageManager()。hasSystemFeature(PackageManager.FEATURE_CAMERA))
返回true;
其他
返回false;
}
}
2.Mysqlitehelper
公共类Mysqlitehelper扩展SQLiteOpenHelper {
公共静态最后弦乐Table_grid =gridtable;
公共静态最后弦乐COLUMN_ID =ID;
公共静态最后弦乐column_URI =URI;
公共静态最后弦乐DB_NAME =Griddb;
公共静态最终诠释版本= 1;
公共静态最后弦乐Database_create =创建表+ Table_grid +(
+ COLUMN_ID +整数主键自动增量,
+ column_URI +文字NOT NULL);;
公共Mysqlitehelper(上下文的背景下){
超(背景下,DB_NAME,空,版本);
}
@覆盖
公共无效的onCreate(SQLiteDatabase DB){
db.execSQL(Database_create);
}
@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
db.execSQL(DROP TABLE IF EXISTS+ Table_grid);
的onCreate(DB);
}
}
3.customiseadapter
公共类customiseadapter扩展了BaseAdapter {
上下文语境;
ArrayList的<字符串>清单;
公共customiseadapter(上下文的背景下,ArrayList的<字符串>列表){
超();
this.context =背景;
this.list =清单;
}
@覆盖
公众诠释getCount将(){
// TODO自动生成方法存根
返回则为list.size();
}
@覆盖
公共对象的getItem(INT为arg0){
// TODO自动生成方法存根
返回list.get(为arg0);
}
@覆盖
众长getItemId(INT为arg0){
// TODO自动生成方法存根
返回将arg0;
}
@覆盖
公共查看getView(INT为arg0,查看ARG1,ARG2的ViewGroup){
ImageView的形象=新ImageView的(上下文);
开放的我们的uri = Uri.parse(list.get(arg0中));
image.setImageURI(URI);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
image.setLayoutParams(新GridView.LayoutParams(70,70));
返回形象;
}
}
-
Gridsource
公共类Gridsource { Mysqlitehelper mydbhelper; SQLiteDatabase数据库; 的String [] COLUMN_NAME = {Mysqlitehelper.column_ID,Mysqlitehelper.column_URI}; 公共Gridsource(上下文的背景下){ mydbhelper =新Mysqlitehelper(上下文); } 公共无效的open() { 数据库= mydbhelper.getWritableDatabase(); } 公共无效的close() { mydbhelper.close(); } 公共无效createpicture(字符串路径) { ContentValues值=新ContentValues(); value.put(Mysqlitehelper.column_URI,路径); database.insert(Mysqlitehelper.Table_grid,空,价值); } 公众的ArrayList<字符串> getallpath() { ArrayList的<字符串> pathlist =新的ArrayList<字符串>(); 光标光标= database.query(Mysqlitehelper.Table_grid,列名,NULL,NULL,NULL,NULL,NULL,NULL); 如果(cursor.moveToFirst()) 做{ 字符串路径=新的String(); 路径= cursor.getString(1); pathlist.add(路径); }而(cursor.moveToNext()); cursor.close(); 返回pathlist; } }
请帮我找到解决办法
您适配器需要的工作。在您的getView()方法,你假设参数是一个URI。事实上,参数是:
公开查看getView(最终诠释的立场,观点convertView,最终的ViewGroup父){
...
}
第一个参数,就是在GridView的项目,你将要呈现的位置。所述第二是查看 - 其可以被回收。而第三个是视图的父。您正在试图采取的立场,一个简单的整数,并使用它作为一个URI
您应该将getView内做的是拉对应于位置个项的图像从你的数据库
请参阅rel="nofollow">这里这个链接的发展装载机您的适配器的详细信息
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;
}
}
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
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
这篇关于我不能够在网格视图设置多个图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!