Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何检索存储在数据库中的图像路径并在图像视图中显示?_Android_Android Imageview - Fatal编程技术网

Android 如何检索存储在数据库中的图像路径并在图像视图中显示?

Android 如何检索存储在数据库中的图像路径并在图像视图中显示?,android,android-imageview,Android,Android Imageview,我正在做一个项目,用户选择图像,我们必须存储图像的路径。我可以在imageView中显示图片,但当应用程序关闭或用户按下后退按钮时,图像不会显示。所以我必须永远保存图像,直到用户删除图像 以下是我的代码: Button buttonLoadImage = (Button) findViewById(R.id.pre1); buttonLoadImage.setOnClickListener(new View.OnClickListener() { @Ov

我正在做一个项目,用户选择图像,我们必须存储图像的路径。我可以在
imageView
中显示图片,但当应用程序关闭或用户按下后退按钮时,图像不会显示。所以我必须永远保存图像,直到用户删除图像

以下是我的代码:

Button buttonLoadImage = (Button) findViewById(R.id.pre1);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });
    }


    @SuppressWarnings("unused")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            Bitmap yourSelectedImage = BitmapFactory.decodeFile(picturePath);

            //testimage.setImageBitmap(yourSelectedImage);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();

            ImageView imageView = (ImageView) findViewById(R.id.im2);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

            pd.open();
            pd.insert(picturePath);
            pd.close();
        }
    }

你可以做两件事:-

  • 如果您希望映像保持在其原始位置,则可以仅使用其路径,并将该路径保存到某个位置,如
    SharedReference
    fileSystem
    、sqlite DB或任何var
  • 另一种方法是将该映像复制到应用程序文件中,然后使用该映像
  • 如果您有图像路径,则可以获得如下图像:-

    private void loadImageFromStorage(String path)
        {
    
            try {
    
    //  if you want to use the same path then use this
    Bitmap b = BitmapFactory.decodeFile(pathName);
                    ImageView img=(ImageView)findViewById(R.id.imgPicker);
                img.setImageBitmap(b);
            } 
            catch (FileNotFoundException e) 
            {
                e.printStackTrace();
            }
    
        }
    

    不要忘记授予权限。通常,当你想在数据库中保存你的图像时,它有很多方法来存储它

    1) 您可以将图像另存为数据库中的imageName

    2) 您还可以将图像的路径保存到数据库中,当您从数据库中以字符串形式检索该路径后,您可以解码该路径,通常我们从gallery提取时可以这样做


    3) 您可以将图像保存为数据库中的字节[],当您要检索图像时,可以将其检索为BLOB图像,然后将该字节[]转换为位图,并将该位图设置为ImageView。

    您可以将图像路径存储在某个位置,如sqlite Db或sharedpref,或者文件系统我正在sqlite db中存储路径…现在,当用户返回时,我如何再次显示它..什么是profile.jpg…我们如何获取图片的名称?查看是否要在应用程序数据中保留该图像,然后可以创建新文件