我想在imageView上显示图像,我将路径存储在数据库中,并希望使用该路径在imageView上显示图像。
String path = Utility.GetColumnValue(testdata, "path");
这会返回路径形式的值>> /抽拉/图像
然后
ImageView img = (ImageView) findViewById(R.id.imageview);
img.setImageURI(Uri.parse(path));
但我的应用程序崩溃了一些错误
答案 0 :(得分:2)
您可以从路径中获取位图
Bitmap bmp = BitmapFactory.decodeFile(img_path );
img.setImageBitmap(bmp);
或者可以尝试这个
String path= "@drawable/myresource.png";
int imageResource = getResources().getIdentifier(path, null, getPackageName());
Drawable res = getResources().getDrawable(imageResource);
img.setImageDrawable(res);
答案 1 :(得分:1)
试试这个:
ImageView img = (ImageView) findViewById(R.id.imageview);
InputStream inputStream = getClass().getResourceAsStream(path);
img.setImageDrawable(Drawable.createFromStream(inputStream, ""));
答案 2 :(得分:0)
Bitmap bmp=BitmapFactory.decodeFile(path);
img.setImageBitmap(bmp);
答案 3 :(得分:0)
package com.example.database_db_hari;
import java.io.ByteArrayOutputStream;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
ImageView iv;
Button b1, b2;
byte[] img1, image;
Bitmap b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.btn_activity_main_open);
b2 = (Button) findViewById(R.id.btn_activity_main_show);
iv = (ImageView) findViewById(R.id.imageView1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DataBaseHub dbh = new DataBaseHub(MainActivity.this);
SQLiteDatabase db = dbh.getWritableDatabase();
ContentValues cv = new ContentValues();
// convert from bitmap to byte array
b = BitmapFactory.decodeResource(getResources(),
R.drawable.aass);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] img1 = stream.toByteArray();
cv.put(DataBaseHub.PHOTO, img1);
db.insert(DataBaseHub.TABLENAME, null, cv);
Toast.makeText(MainActivity.this, "Insert Success",
Toast.LENGTH_SHORT).show();
}
});
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DataBaseHub dbh = new DataBaseHub(MainActivity.this);
SQLiteDatabase db = dbh.getReadableDatabase();
// convert from byte array to bitmap
Cursor c = db.query(DataBaseHub.TABLENAME, null, null, null,
null, null, null);
if (c.moveToFirst()) {
do {
image = c.getBlob(c.getColumnIndex("photo"));
} while (c.moveToNext());
}
Bitmap bmp = BitmapFactory.decodeByteArray(image, 0,
image.length);
iv.setImageBitmap(bmp);
Toast.makeText(MainActivity.this, "Show Success",
Toast.LENGTH_SHORT).show();
}
});
}
}