【问题标题】:How to save the gallery image Android?如何保存画廊图像Android?
【发布时间】:2016-01-08 08:28:43
【问题描述】:

我想在我的应用程序中显示用户图像,所以我在导航抽屉中拍摄了一个图像视图。

首先我从图库中获取图像并将该图像设置为图像视图。但是每当我关闭应用程序并打开图像时,图像都没有显示(我从图库中选择的)。所以我想将图像保存在内存中。

我试过了,但图片没有显示。

请任何人帮助我。

提前谢谢你。

我的代码:

  rightimage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

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

                }
            });

     @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == RESULT_LOAD_IMAGE  && null != data) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };
                Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                rightimage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    //saving image in internal storage.
                saveImageToInternalStorage(BitmapFactory.decodeFile(picturePath));
            }
        }
    // //saving image in internal storage.

        public boolean saveImageToInternalStorage(Bitmap image) {

            try {
    // Use the compress method on the Bitmap object to write image to
    // the OutputStream
                 fos = getActivity().openFileOutput(file, Context.MODE_PRIVATE);

    // Writing the bitmap to the output stream
                image.compress(Bitmap.CompressFormat.PNG, 100, fos);
                fos.close();

                return true;
            } catch (Exception e) {
                Log.e("saveToInternalStorage()", e.getMessage());
                return false;
            }
        }


        public Bitmap getThumbnail(String filename) {


            Bitmap thumbnail = null;



    // If no file on external storage, look in internal storage

                try {
                    Log.v("RTAG_IMAG",""+"IMG");
                    File filePath = getActivity().getFileStreamPath(filename);
                    FileInputStream fi = new FileInputStream(filePath);
                    thumbnail = BitmapFactory.decodeStream(fi);
                    rightimage.setImageBitmap(thumbnail);
                } catch (Exception ex) {
                    Log.e("getThumbnail()", ex.getMessage());
                }

            return thumbnail;

        }

【问题讨论】:

  • 请您为此发布您的 logcat 日志。

标签: android imageview android-gallery internal-storage


【解决方案1】:
  1. 为 user_image 使用 sharedPref 维护一个标志。
  2. 如果已设置,则将 user_image 从存储的文件设置为 ImageView
  3. 另外加载一些默认的 profile_image

【讨论】:

    【解决方案2】:

    试试这个方法。

    private String saveToInternalSorage(Bitmap bitmapImage){
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
         // path to /data/data/yourapp/app_data/imageDir
        File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
        // Create imageDir
        File mypath=new File(directory,"profile.jpg");
    
        FileOutputStream fos = null;
        try {           
            fos = new FileOutputStream(mypath);
       // Use the compress method on the BitMap object to write image to the OutputStream
            bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
        } catch (Exception e) {
              e.printStackTrace();
        } finally {
              fos.close(); 
        } 
        return directory.getAbsolutePath();
    }
    

    } 1.目录将以给定的名称创建。 Javadocs 用于告诉它将在哪里创建目录。

    2.您必须提供要保存的图像名称。

    并像这样加载它的保存图像。

    private void loadImageFromStorage(String path)
    

    {

    try {
        File f=new File(path, "profile.jpg");
        Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
            ImageView img=(ImageView)findViewById(R.id.imgPicker);
        img.setImageBitmap(b);
    } 
    catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    }
    

    【讨论】:

    • 我需要为 loadImageFromStorage 提供哪个路径
    • @kartheekij 阅读 saveToInternalSorage() 方法中的注释。该路径用于内部存储,您可以在保存时保存它。请参阅方法返回 directory.getAbsolutePath()。它是保存文件的目录的路径
    猜你喜欢
    • 2012-01-23
    • 1970-01-01
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 2016-08-06
    相关资源
    最近更新 更多