如何解决这些问题?
我不知道接下来会发生什么。
我解决了这些问题,但你不知道它不起作用
代码
主要的.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/="vertical" ="fill_parent"
android:layout_height="fill_parent"
android:background="#DCDCDC">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="welcome"
android:textColor="#000000"
/>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/icon"
/>
<Button
android:id="@+id/btn_submit"
android:text="Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/img2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/icon"/>
<Button
android:id="@+id/btn_retrieve"
android:text="Retrieve"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
公共类.java
package com.example.demomediaproject;
import android.app.Application;
public class CommonClass extends Application{
byte[] profileImageInBytes;
}
数据库方法.java
package com.example.demomediaproject;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseMethods {
private static final String databaseName = "DemoDb";
private static final int databaseVersion = 1;
private static final String projectCustomProfiles =
"CREATE TABLE IF NOT EXISTS PROJECT_CUSTOM_PROFILES (PROFILE_IMAGE VARBINARY(MAX));";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
Cursor cursor;
int count;
boolean isProjectNamePresent=false;
boolean isProfileNamePresent = false;
public DatabaseMethods(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
public DatabaseHelper(Context context)
{
super(context, databaseName, null, databaseVersion);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(projectCustomProfiles);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS PROJECT_CUSTOM_PROFILES");
onCreate(db);
}
}
//---opens the database---
public DatabaseMethods open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
public void storeCustomProfiles(byte[] imgData)
{
String query;
query = "INSERT OR REPLACE INTO PROJECT_CUSTOM_PROFILES (PROFILE_IMAGE)
VALUES('"+imgData+"')";
db.execSQL(query);
}
public Cursor fetchProfileImageFromDatabase()
{
return db.rawQuery("SELECT PROFILE_IMAGE FROM PROJECT_CUSTOM_PROFILES " , null);
}
public int getCountForCustomProfiles()
{
cursor= db.rawQuery("SELECT COUNT(PROFILE_IMAGE) FROM PROJECT_CUSTOM_PROFILES",null);
if(cursor.moveToFirst())
{
count= cursor.getInt(0);
}
cursor.close();
return count;
}
}
DemomediaprojectActivity.java
package com.example.demomediaproject;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.MediaColumns;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class DemomediaprojectActivity extends Activity implements OnClickListener {
ImageView pic,pic1;
Button submitButton,retrieveButton;
private static final int CAMERA_PIC_REQUEST = 1337;
private static final int SELECT_PICTURE = 1;
String fileManagerString,imagePath;
String selectedImagePath="";
int columnIndex;
CommonClass commClass;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pic=(ImageView)findViewById(R.id.img);
pic.setOnClickListener(this);
pic1=(ImageView)findViewById(R.id.img2);
submitButton=(Button)findViewById(R.id.btn_submit);
submitButton.setOnClickListener(this);
retrieveButton=(Button)findViewById(R.id.btn_retrieve);
retrieveButton.setOnClickListener(this);
commClass = ((CommonClass) getApplicationContext());
DatabaseMethods db=new DatabaseMethods(this);
db.open();
}
@Override
public void onClick(View arg0)
{DatabaseMethods db=new DatabaseMethods(this);
// TODO Auto-generated method stub
switch (arg0.getId())
{
case R.id.img:
showalert();
break;
case R.id.btn_submit:
db.open();
db.storeCustomProfiles(commClass.profileImageInBytes);
db.close();
break;
case R.id.btn_retrieve:
db.open();
Cursor cursor = db.fetchProfileImageFromDatabase();
if(cursor != null)
{
if(cursor.moveToFirst())
{
do
{
//byte[] data = cursor.getBlob(cursor.getColumnIndex("PROFILE_IMAGE"));
//pic1.setImageBitmap(BitmapFactory.decodeByteArray(data, 0, data.length));
//String temp=cursor.getBlob(cursor.getColumnIndex("PROFILE_IMAGE")).toString();
//byte[] data=temp.getBytes();
//pic1.setImageBitmap(BitmapFactory.decodeByteArray(data, 0, data.length));
byte[] data = cursor.getBlob(cursor.getColumnIndex("PROFILE_IMAGE"));
ByteArrayInputStream imageStream = new ByteArrayInputStream(data);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
// pic1.setImageBitmap(theImage);
}
while(cursor.moveToNext());
}
cursor.close();
}
db.close();
break;
default:
break;
}
}
/*private byte[] getBitmapAsByteArray(Bitmap theImage)
{
// TODO Auto-generated method stub
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// Middle value is quality, but PNG is lossless, so it's ignored.
theImage.compress(CompressFormat.PNG, 0, outputStream);
return outputStream.toByteArray();
}*/
private void showalert()
{
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Photo Source")
.setMessage("Select Pictures From Media Library")
.setCancelable(false)
.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNeutralButton("Select Picture", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id1) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "select picture"), SELECT_PICTURE);
}
})
.setNegativeButton("New", new DialogInterface.OnClickListener() {
//camera function call
public void onClick(DialogInterface dialog, int id2) {
// TODO Auto-generated method stub
Intent intentCamera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intentCamera,CAMERA_PIC_REQUEST);
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{ if (resultCode == Activity.RESULT_OK)
{ if(requestCode == SELECT_PICTURE)
{
Uri selectedImageUri = data.getData();
//OI FILE Manager
fileManagerString = selectedImageUri.getPath();
//MEDIA GALLERY
selectedImagePath =getPath(selectedImageUri);
pic.setImageURI(selectedImageUri);
imagePath.getBytes();
}
if(requestCode == CAMERA_PIC_REQUEST)
{
Bitmap image = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
CommonClass commClass = ((CommonClass)getApplicationContext());
commClass.profileImageInBytes = stream.toByteArray();
pic.setImageBitmap(BitmapFactory.decodeByteArray(commClass.profileImageInBytes, 0,
commClass.profileImageInBytes.length));
}
}
}
private String getPath(Uri uri)
{
// TODO Auto-generated method stub
String[] projection = { MediaColumns.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
columnIndex = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
imagePath = cursor.getString(columnIndex);
return cursor.getString(columnIndex);
}
}