如何根据数据库中的路径设置存储在文件系统中的图像视图?

发布于 2024-11-14 16:21:43 字数 3819 浏览 2 评论 0原文

我有三个问题:

  1. 如果我选择将 image.png 存储在文件系统中,我应该将其存储在哪里 res/drawable/image_1.pngres /drawable/images/image_1.png

  2. 我将把图像的路径存储在数据库中。我应该在 image_path 字段中输入什么内容,例如。 image_1images/image_1 或等等

  3. 如何从数据库中获取图像路径并设置要查看的图像,遵循我在底部的代码?你们能帮我改一下吗?

,我已经有了答案。

  1. 如果将图像文件存储在 assets/images/pic_1.png 的文件系统中
  2. 在数据库的“image_path 字段”中,您将放置 images/pic_1.png在其中。
  3. 获取和设置图像:根据Trim的回答。

我已经根据 Trim 的回答修复了以下代码。

非常感谢

placeListActivity.class

public class placeListActivity extends ListActivity {
   
    private static MyDB mDbHelper;
    String[] from = new String[] { Constants.COL_TITLE};
    int[] to = new int[] {R.id.list_place_title};
    private Cursor c;

    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
        mDbHelper = new MyDB(this);
        mDbHelper.createDatabase();
        mDbHelper.open();
        c = mDbHelper.getAllPlaces();

        
        setListAdapter(new SimpleCursorAdapter(this, 
                  R.layout.list_place, c, 
                  from, to));
        
        final ListView lv = getListView();
          
        
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

                Intent i = new Intent(view.getContext(), Place.class);    
                i.putExtra(Constants.KEY_ID, id);
                i.putExtra(Constants.COL_TITLE, c.getString(
                        c.getColumnIndexOrThrow(Constants.COL_TITLE)));
                i.putExtra(Constants.COL_CONTENT, c.getString(
                        c.getColumnIndexOrThrow(Constants.COL_CONTENT)));
 
                i.putExtra(Constants.COL_IMAGE, c.getString(
                        c.getColumnIndexOrThrow(Constants.COL_IMAGE)));
                
                
                
                startActivity(i);
            
            }
          });
        
        
    }

}

Place.class

public class Place extends Activity {
    
    private TextView title;
    private TextView content;
    private ImageView placeImage;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.detail_place);
        
        title = (TextView) findViewById(R.id.place_title);
        content = (TextView) findViewById(R.id.place_content);

        placeImage = (ImageView) findViewById(R.id.place_image);
        
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
        // reference XML defined views that we will touch in code
        String stringTitle = extras.getString(Constants.COL_TITLE);
        String stringContent = extras.getString(Constants.COL_CONTENT);

        String imageID = extras.getString(Constants.COL_IMAGE);
        
 
        if (title != null) {
            title.setText(stringTitle); 
        }
        if (content != null) {
            content.setText(stringContent);
        }

        /*if (placeImage != null) {
            placeImage.setImageDrawable(Drawable.createFromPath(imageID));
        }*/
        
        if (placeImage != null) {
        
        try {
            InputStream path = getAssets().open(imagePath);
            Bitmap bit = BitmapFactory.decodeStream(path);
            placeImage.setImageBitmap(bit);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            }
        }
    }
}

I have three questions:

  1. If I choose to store image.png in file system where should I store it res/drawable/image_1.png or res/drawable/images/image_1.png

  2. And I'm going to store path of image in database. What should I put in image_path field ex. image_1 or images/image_1 or etc.

  3. How can I get path of image from database and set image to view follow my code in the bottom? Could you guys change it for me?

I already have answer

  1. in case storing image file in file system in assets/images/pic_1.png
  2. In database, "image_path field" , you will put images/pic_1.png in it.
  3. To get and set image: according to Trim's answer.

AND I have fixed the following code according to Trim's answer.

Thanks so much

placeListActivity.class

public class placeListActivity extends ListActivity {
   
    private static MyDB mDbHelper;
    String[] from = new String[] { Constants.COL_TITLE};
    int[] to = new int[] {R.id.list_place_title};
    private Cursor c;

    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
        mDbHelper = new MyDB(this);
        mDbHelper.createDatabase();
        mDbHelper.open();
        c = mDbHelper.getAllPlaces();

        
        setListAdapter(new SimpleCursorAdapter(this, 
                  R.layout.list_place, c, 
                  from, to));
        
        final ListView lv = getListView();
          
        
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

                Intent i = new Intent(view.getContext(), Place.class);    
                i.putExtra(Constants.KEY_ID, id);
                i.putExtra(Constants.COL_TITLE, c.getString(
                        c.getColumnIndexOrThrow(Constants.COL_TITLE)));
                i.putExtra(Constants.COL_CONTENT, c.getString(
                        c.getColumnIndexOrThrow(Constants.COL_CONTENT)));
 
                i.putExtra(Constants.COL_IMAGE, c.getString(
                        c.getColumnIndexOrThrow(Constants.COL_IMAGE)));
                
                
                
                startActivity(i);
            
            }
          });
        
        
    }

}

Place.class

public class Place extends Activity {
    
    private TextView title;
    private TextView content;
    private ImageView placeImage;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.detail_place);
        
        title = (TextView) findViewById(R.id.place_title);
        content = (TextView) findViewById(R.id.place_content);

        placeImage = (ImageView) findViewById(R.id.place_image);
        
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
        // reference XML defined views that we will touch in code
        String stringTitle = extras.getString(Constants.COL_TITLE);
        String stringContent = extras.getString(Constants.COL_CONTENT);

        String imageID = extras.getString(Constants.COL_IMAGE);
        
 
        if (title != null) {
            title.setText(stringTitle); 
        }
        if (content != null) {
            content.setText(stringContent);
        }

        /*if (placeImage != null) {
            placeImage.setImageDrawable(Drawable.createFromPath(imageID));
        }*/
        
        if (placeImage != null) {
        
        try {
            InputStream path = getAssets().open(imagePath);
            Bitmap bit = BitmapFactory.decodeStream(path);
            placeImage.setImageBitmap(bit);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手长情犹 2024-11-21 16:21:43

您可以将图像放在“assets/images”目录下。在这种情况下,您将在 getAssets().open(String path) 中使用的路径将类似于 “images/pic_1.png”。
您可以在 Activity 中的任何位置调用 getAssets()
还有一个 setImageBitmap(Bitmap bm) 方法。
您可以通过 BitmapFactory.decodeFile(String path) 从路径创建位图。

You can put your images under "assets/images" directory. In this case, the path that you will use in getAssets().open(String path) will be like "images/pic_1.png".
You can call getAssets() anywhere in your activity.
There is also a setImageBitmap(Bitmap bm) method.
You can create bitmap from path via BitmapFactory.decodeFile(String path).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文