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

标签 java android sqlite camera datatableadapters

我遵循 Android 开发人员的示例,从相机 Intent 中获取图像并将其放置在您的 View 中。当我尝试将该图像 uri 保存在我的 SqliteDatabase 上(只是该图像的链接而不是完整图像,因此我节省空间)然后我尝试在我的自定义适配器上恢复它时,问题就开始了。 p>

链接到google dev -> http://developer.android.com/training/camera/index.html

我尝试过,但没有成功

创建了一个全局字符串 Logo ,并将其放入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();

然后我将 Logo 存储在 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方法中

将此方法添加到您的 Activity 中

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是所选图像的路径

希望能帮到你..

关于java - Android 从相机获取图像 Uri,存储在 SQLite 中并将其恢复到我的自定义适配器上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11843892/

相关文章:

android - 合并的 SQL 查询问题

java - 从另一个方法访问位置变量

java - 使用 JDBC 获取所有外键

java - 在 Eclipse 插件项目中使用来自普通旧 Java 项目的包

javascript - 确定并绑定(bind)点击或 "touch"事件

sql - 如何使用 DB Browser SQLite 在列中添加批量值

sqlite - 如何让 INSERT OR IGNORE 工作

java - Android:并发后台作业的设计(Service、IntentService 还是 AsyncTask?)

不同模块中具有相同布局的Android数据绑定(bind)

javascript - 如何在后台运行cocos Creator的android构建