http://download.csdn.net/detail/scorpion_0/4076156这是我的项目晕啊 保存第二张图片总是出错啊
不知道是为什么啊package com.android.openpicture;import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class OpenPicture extends Activity {
/** Called when the activity is first created. */
public static final String DB_NAME = "code.db";
public static final int VERSION = 1;
public static MyHelper HELPER;
public static SQLiteDatabase DB;
int id = 0 ;
List<Bitmap> list = new ArrayList<Bitmap>();
private LinearLayout linearLayoutMain = null ;
ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); try {
//初始化数据库辅助对象
HELPER= new MyHelper(this, DB_NAME, null, VERSION); //获得可读写的SQLiteDatabase对象
DB = HELPER.getWritableDatabase(); } catch (Exception e2) {
// T ODO Auto-generated catch block
//e2.printStackTrace();
System.exit(0);
}
linearLayoutMain = (LinearLayout) findViewById (R.id.linear) ;
Button top = (Button)findViewById(R.id.top);
top.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
}});
Button down = (Button)findViewById(R.id.down);
down.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
}});
Button button = (Button)findViewById(R.id.b01);
button.setText("选择图片");
button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
/* 开启Pictures画面Type设定为image */
intent.setType("image/*");
/* 使用Intent.ACTION_GET_CONTENT这个Action */
intent.setAction(Intent.ACTION_GET_CONTENT);
/* 取得相片后返回本画面 */
startActivityForResult(intent, 1);
}});
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
ContentResolver cr = this.getContentResolver();
try {
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
// ImageView imageView = (ImageView) findViewById(R.id.iv01);
/* 将Bitmap设定到ImageView */
list.add(bitmap);
LinearLayout linearLayoutAdd = (LinearLayout) getLayoutInflater().inflate(R.layout.imageviews, linearLayoutMain) ;
imageView = ((ImageView) linearLayoutAdd.findViewById(R.id.iv));
imageView.setId(list.size()-1);
//imageView.setImageBitmap(list.get(list.size()-1));
bitmap = list.get(list.size()-1);
saveIcon(bitmap);//存图片
dis();//取图片 } catch (FileNotFoundException e) {
Log.e("Exception", e.getMessage(),e);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
public void saveIcon(Bitmap bm) {
String sql = "select max("
+ MyHelper.ID
+ ") from "
+ MyHelper.TB_NAME ; Cursor Cur = null;
try {
Cur = DB.rawQuery(sql, null);
} catch (Exception e) {}
if(Cur != null && Cur.getCount() > 0)
{
Cur.moveToFirst();
id = Cur.getInt(0);
}
if (bm == null)
{
return;
}
id ++ ;
byte[] bbm = Bitmap2Bytes(bm);
ContentValues values = new ContentValues();
values.put(MyHelper.ID, id);
values.put(MyHelper.PATH, bbm);
if(DB.insert(MyHelper.TB_NAME, null, values) == 1)
{
Log.e("状态", "成功" + id);
}
else
{
Log.e("状态", "失败" + id);
}
/*
sql = "insert into "
+ MyHelper.TB_NAME
+ " values ('"
+ id + "','"
+ bbm + "')";
Log.e("sql", sql);
DB.execSQL(sql);
*/
} public void dis()
{
String sql = "select "
+ MyHelper.PATH
+ " from "
+ MyHelper.TB_NAME
+ " where "
+ MyHelper.ID
+ " = "
+ id; Cursor Cur = null;
try {
Cur = DB.rawQuery(sql, null);
} catch (Exception e) {
}
if(Cur != null && Cur.getCount() > 0)
{
Cur.moveToFirst();
byte[] blob = Cur.getBlob(Cur.getColumnIndex(MyHelper.PATH));
Bitmap bmp = Bytes2Bimap(blob);
if(bmp != null)
imageView.setImageBitmap(bmp);
else
Log.e("dis", "null");
}
} private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
private Bitmap Bytes2Bimap(byte[] b){
if(b != null && b.length != 0){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
else {
return null;
}
}
}
不知道是为什么啊package com.android.openpicture;import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class OpenPicture extends Activity {
/** Called when the activity is first created. */
public static final String DB_NAME = "code.db";
public static final int VERSION = 1;
public static MyHelper HELPER;
public static SQLiteDatabase DB;
int id = 0 ;
List<Bitmap> list = new ArrayList<Bitmap>();
private LinearLayout linearLayoutMain = null ;
ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); try {
//初始化数据库辅助对象
HELPER= new MyHelper(this, DB_NAME, null, VERSION); //获得可读写的SQLiteDatabase对象
DB = HELPER.getWritableDatabase(); } catch (Exception e2) {
// T ODO Auto-generated catch block
//e2.printStackTrace();
System.exit(0);
}
linearLayoutMain = (LinearLayout) findViewById (R.id.linear) ;
Button top = (Button)findViewById(R.id.top);
top.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
}});
Button down = (Button)findViewById(R.id.down);
down.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
}});
Button button = (Button)findViewById(R.id.b01);
button.setText("选择图片");
button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
/* 开启Pictures画面Type设定为image */
intent.setType("image/*");
/* 使用Intent.ACTION_GET_CONTENT这个Action */
intent.setAction(Intent.ACTION_GET_CONTENT);
/* 取得相片后返回本画面 */
startActivityForResult(intent, 1);
}});
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
ContentResolver cr = this.getContentResolver();
try {
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
// ImageView imageView = (ImageView) findViewById(R.id.iv01);
/* 将Bitmap设定到ImageView */
list.add(bitmap);
LinearLayout linearLayoutAdd = (LinearLayout) getLayoutInflater().inflate(R.layout.imageviews, linearLayoutMain) ;
imageView = ((ImageView) linearLayoutAdd.findViewById(R.id.iv));
imageView.setId(list.size()-1);
//imageView.setImageBitmap(list.get(list.size()-1));
bitmap = list.get(list.size()-1);
saveIcon(bitmap);//存图片
dis();//取图片 } catch (FileNotFoundException e) {
Log.e("Exception", e.getMessage(),e);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
public void saveIcon(Bitmap bm) {
String sql = "select max("
+ MyHelper.ID
+ ") from "
+ MyHelper.TB_NAME ; Cursor Cur = null;
try {
Cur = DB.rawQuery(sql, null);
} catch (Exception e) {}
if(Cur != null && Cur.getCount() > 0)
{
Cur.moveToFirst();
id = Cur.getInt(0);
}
if (bm == null)
{
return;
}
id ++ ;
byte[] bbm = Bitmap2Bytes(bm);
ContentValues values = new ContentValues();
values.put(MyHelper.ID, id);
values.put(MyHelper.PATH, bbm);
if(DB.insert(MyHelper.TB_NAME, null, values) == 1)
{
Log.e("状态", "成功" + id);
}
else
{
Log.e("状态", "失败" + id);
}
/*
sql = "insert into "
+ MyHelper.TB_NAME
+ " values ('"
+ id + "','"
+ bbm + "')";
Log.e("sql", sql);
DB.execSQL(sql);
*/
} public void dis()
{
String sql = "select "
+ MyHelper.PATH
+ " from "
+ MyHelper.TB_NAME
+ " where "
+ MyHelper.ID
+ " = "
+ id; Cursor Cur = null;
try {
Cur = DB.rawQuery(sql, null);
} catch (Exception e) {
}
if(Cur != null && Cur.getCount() > 0)
{
Cur.moveToFirst();
byte[] blob = Cur.getBlob(Cur.getColumnIndex(MyHelper.PATH));
Bitmap bmp = Bytes2Bimap(blob);
if(bmp != null)
imageView.setImageBitmap(bmp);
else
Log.e("dis", "null");
}
} private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
private Bitmap Bytes2Bimap(byte[] b){
if(b != null && b.length != 0){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
else {
return null;
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货