如何将avd中sd卡中的图像存储到android中的数据库中?

发布于 2024-11-07 19:56:50 字数 223 浏览 0 评论 0原文

我是安卓新手。我刚刚在 Android 2.1 中创建了一个带有 256 MB android-SDcard 的 AVD。我已经在其中插入了两张图像。我使用 DDMS 视角完成了此操作。图像现在存储在SD卡的DCIM文件夹中的文件夹100ANDRO中。现在我想创建一个应用程序,允许用户通过浏览文件夹来选择图像,并需要将相应的图像保存到数据库android-sqlite中。

有人可以帮我找到合适的方法吗?提前致谢。

I am new to Android. I have just created an AVD with 256 MB android-SDcard in it in Android 2.1. And I have inserted two images into it. I have done this using the DDMS perspective. And the images are now stored into a folder 100ANDRO in the DCIM folder of SDcard. Now I want to create an application that allows the user to select the images through browsing the folders and need to save the corresponding image to the database android-sqlite.

Can someone help me to find an appropriate method for this? Thanks in advance.

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

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

发布评论

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

评论(1

最舍不得你 2024-11-14 19:56:50

我找到了一种方法。

我创建了一个上传按钮,并在单击操作上设置了这样的按钮。

    upload.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, 1);

        }
    });

我已经重写了这个方法以及下面相同的类。

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case 1:
     {
      if (resultCode == RESULT_OK)
      {
        Uri photoUri = data.getData();
        if (photoUri != null)
        {
        try {
              String[] filePathColumn = {MediaStore.Images.Media.DATA};
              Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null); 
         cursor.moveToFirst();
     int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
     String filePath = cursor.getString(columnIndex);
     cursor.close();
     Bitmap bitmap = BitmapFactory.decodeFile(filePath);
     imgView.setImageBitmap(bitmap);
     int size = bitmap.getWidth() * bitmap.getHeight();
     ByteArrayOutputStream out = new ByteArrayOutputStream(size);
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
     try {
     out.flush();
     out.close();
     } catch (IOException e) {
     e.printStackTrace();}
     String bb = out.toString();
     byte[] x = out.toByteArray();
     image_value.setTag(x);
     image_value.setText(filePath);
     }catch(Exception e)
      {}
      }
    }
    }

这里image_value代表xml文件中的隐藏文本视图。
我已将图像位置和字节的值作为文本视图的值和标记传递。
后来我将这些字节保存到数据库中以供以后显示。它工作正常。

感谢大家。

I have found one method for this.

I have created a button for UPLOAD and on the click action I have set like this.

    upload.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, 1);

        }
    });

And I have overrided this method along with the same class as below.

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case 1:
     {
      if (resultCode == RESULT_OK)
      {
        Uri photoUri = data.getData();
        if (photoUri != null)
        {
        try {
              String[] filePathColumn = {MediaStore.Images.Media.DATA};
              Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null); 
         cursor.moveToFirst();
     int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
     String filePath = cursor.getString(columnIndex);
     cursor.close();
     Bitmap bitmap = BitmapFactory.decodeFile(filePath);
     imgView.setImageBitmap(bitmap);
     int size = bitmap.getWidth() * bitmap.getHeight();
     ByteArrayOutputStream out = new ByteArrayOutputStream(size);
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
     try {
     out.flush();
     out.close();
     } catch (IOException e) {
     e.printStackTrace();}
     String bb = out.toString();
     byte[] x = out.toByteArray();
     image_value.setTag(x);
     image_value.setText(filePath);
     }catch(Exception e)
      {}
      }
    }
    }

Here image_value represents a hidden text view in the xml file.
I have passed the value of the image location and bytes as text view's value and tag.
And later on I have saved this bytes into the database for later display. Its working fine.

Thanks to all.

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