这几天完成的内容有:
1.“添加图片”按钮
2.添加图片功能
遇到的问题:
我想要将添加图片按钮放在右下角,所以采用了相对布局,但是问题随之二来,因为将导航栏设置成了半透明,所以图片放到右下角之后,半透明的导航栏会把图片挡住,最后采取的办法是把图片高度变大,导航栏挡住多处的部分,也算是勉强实现要求了,但是对于一些没有虚拟导航栏的手机,按钮距离底部的间隔会显得略大,但是目前技术实在想不出更好的办法了
在Activity Action里面有一个“ACTION_GET_CONTENT”字符串常量,该常量让用户选择特定类型的数据,并返回该数据的URI.我们利用该常量,然后设置类型为“image/*”,就可获得android手机内的所有image。然后再通过startActivityForResult让我们选择的image返回到Activity中去
AddPicActivity.java
package cn.lixyz.tuji;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import cn.lixyz.tuji.Util.GetDate;
import cn.lixyz.tuji.sqlite.MySQLiteOpenHelper;
/**
* 添加图片类 选择图片之后,先判断用户是否安装SD卡,如果安装,则在SD卡中新建一个cn.lixyz.tuji目录,将选择的图片复制到目录中
*
* @author LGB
*
*/
public class AddPicActivity extends Activity implements View.OnClickListener {
private ImageView iv\_select\_img;
private EditText et\_image\_name, et\_image\_tag, et\_image\_remark;
private Button bt\_submit;
private Bitmap bitmap;
private File sdPath;
private String fileName = "TUJIE" + new Date().getTime();
private FileOutputStream fos;
private MySQLiteOpenHelper msoh;
private SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\_addpic);
initView();
}
private void initView() {
iv\_select\_img = (ImageView) findViewById(R.id.iv\_select\_img);
et\_image\_name = (EditText) findViewById(R.id.et\_image\_name);
et\_image\_tag = (EditText) findViewById(R.id.et\_image\_tag);
et\_image\_remark = (EditText) findViewById(R.id.et\_image\_remark);
bt\_submit = (Button) findViewById(R.id.bt\_submit);
iv\_select\_img.setOnClickListener(this);
bt\_submit.setOnClickListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT\_OK) {
Uri uri = data.getData();
ContentResolver cr = this.getContentResolver();
try {
bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
long i = new Date().getTime();
copyPicToSDCard(bitmap);
Log.d("TTTT", "共耗时:" + (new Date().getTime() - i));
iv\_select\_img.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
Log.e("Exception", e.getMessage(), e);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv\_select\_img:
Intent intent = new Intent();
intent.setType("image/\*");
intent.setAction(Intent.ACTION\_GET\_CONTENT);
startActivityForResult(intent, 1);
break;
case R.id.bt\_submit:
String picName = et\_image\_name.getText().toString().trim();
String picTag = et\_image\_tag.getText().toString().trim();
String picRemark = et\_image\_remark.getText().toString().trim();
msoh = new MySQLiteOpenHelper(AddPicActivity.this, "TujiData", null, 1);
database = msoh.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("picName", picName);
cv.put("picTag", picTag);
cv.put("picRemark", picRemark);
cv.put("fileName", fileName);
cv.put("date", new GetDate().getDate());
database.insert("pics", null, cv);
database.close();
break;
}
}
private void copyPicToSDCard(Bitmap bm) throws IOException {
if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA\_MOUNTED)) {
/\* 得到SD卡得路径 \*/
sdPath = Environment.getExternalStorageDirectory();
try {
File dir = new File(sdPath + "/cn.lixyz.TuJi");
fos = new FileOutputStream(new File(dir + "/" + fileName + ".png"));
if (!dir.exists()) {
dir.mkdir();
bm.compress(Bitmap.CompressFormat.PNG, 100, fos);
} else {
bm.compress(Bitmap.CompressFormat.PNG, 100, fos);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
fos.flush();
fos.close();
}
} else {
Toast.makeText(AddPicActivity.this, "未检测到您的SD卡,长时间使用本软件,会造成系统空间减少", Toast.LENGTH\_SHORT).show();
}
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章