繁体   English   中英

Android从相机获取图像Uri,存储在SQLite中并将其还原到我的自定义适配器上

[英]Android get image Uri from camera, store in SQLite and restore it on my custom adapter

我遵循了Android开发人员的示例,即从相机意图中获取图像并将其放置在视图中。 当我尝试将图像uri保存在SqliteDatabase上时,问题就开始了(只是指向该图像的链接而不是完整图像,因此我节省了空间),然后尝试在customadapter上还原它。

链接到Google开发人员-> http://developer.android.com/training/camera/index.html

我没有成功尝试过

创建了一个全局字符串徽标,并在handleSmallCameraPhoto里面放了这个

private void handleSmallCameraPhoto(Intent intent) {
        Bundle extras = intent.getExtras();
        mImageBitmap = (Bitmap) extras.get("data");
        ImagenViaje.setImageBitmap(mImageBitmap);
        ImagenViaje.setVisibility(View.VISIBLE);
    --> logo = extras.get("data").toString();

然后我将徽标存储在SQLite上,并尝试通过这种方式将其还原到适配器上

String imagePath = c.getString(c.getColumnIndexOrThrow(MySQLiteHelper.COLUMN_LOGO_PATH));

然后

 ImageView item_image = (ImageView) v.findViewById(R.id.logo);
    item_image.setVisibility(ImageView.INVISIBLE);
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath);

        item_image.setImageBitmap(bitmap);
        item_image.setVisibility(ImageView.VISIBLE);

使用获取图像路径

Uri selectedImageUri = intent.getData();
String s1 = intent.getDataString(); 

String selectedImagePath = getPath(selectedImageUri);
if(selectedImagePath==null && s1 != null)
{
    selectedImagePath = s1.replaceAll("file://","");
}

在您的handleSmallCameraPhoto方法中

在您的活动中添加此方法

public String getPath(Uri uri) {

    try{
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    if(cursor==null)
    {
        return null;

    }
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
    }
    catch(Exception e)
    {
        return null;
    }

}

selectedImagePath保存在数据库中,并在需要时使用selectedImagePath是所选图像的路径

希望对你有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM