我正在创建应用程序 从相机拍照。 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;
}
}
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 :(得分:0)
您的适配器需要工作。在你的getView()方法中,你假设参数是一个URI。事实上,论点是:
public View getView(final int position, View convertView, final ViewGroup parent) {
...
}
第一个参数是您要渲染的gridview中项目的位置。第二个是视图 - 可以回收。而第3个是视图的父级。您正在尝试获取位置,一个简单的整数,并将其用作URI
你在getView中应该做的是将数据拉出数据库,该数据对应于“位置”项目
有关为适配器开发加载器的更多信息,请参阅this link here