在Android应用程序中,关闭应用程序并重新启动它后,图像不会显示

时间:2016-12-26 09:36:46

标签: android

在应用程序中,我允许用户从图库中选择图像,或者他可以从相机中选择。虽然我可以在第一次管理图像并在活动中显示它,但在关闭应用程序并重新启动它之后,图像消失了,空间是空白的。有一个解释让我将图像数据保存在sharedPreferences中但是我是android新手,并不是很了解。我寻找sharedPreferences但不知道如何使它工作。 所以,如果有人帮忙解释一些解释和代码,那将对我有所帮助。 谢谢。 这是我试图做的。

    private void openCamera(){
        // create Intent to take a picture and return control to the calling application
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,getPhotoFileUri(photoFileName)); // set the image file name

        // If you call startActivityForResult() using an intent that no app           can handle, your app will crash.
        // So as long as the result is not null, it's safe to use the intent.
            if (intent.resolveActivity(getPackageManager()) != null) {
            // Start the image capture intent to take photo
                startActivityForResult(intent, TAKE_IMAGE);
            }
        }



        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
       // final android.widget.LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageview.getLayoutParams();

        if (requestCode == PICK_IMAGE && resultCode == RESULT_OK) {
            Uri imageUri = data.getData();
            imageview.setImageURI(imageUri);
            //selectedImagePath = getPath(imageUri);
            //ystem.out.println("Image Path : " + selectedImagePath);
        }

        else if (requestCode == TAKE_IMAGE && resultCode == Activity.RESULT_OK) {
            Uri takenPhotoUri = getPhotoFileUri(photoFileName);
            // by this point we have the camera photo on disk
            Bitmap rawTakenImage = BitmapFactory.decodeFile(takenPhotoUri.getPath());
            // RESIZE BITMAP, see section below
            // See BitmapScaler.java: https://gist.github.com/nesquena/3885707fd3773c09f1bb
            // Get height or width of screen at runtime
            int screenWidth = DeviceDimensionsHelper.getDisplayWidth(this);
// Resize a Bitmap maintaining aspect ratio based on screen width

            Bitmap resizedBitmap = BitmapScaler.scaleToFitWidth(rawTakenImage,screenWidth);
            // Load the taken image into a preview
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
// Compress the image further
            resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
// Create a new file for the resized bitmap (`getPhotoFileUri` defined above)
            Uri resizedUri = getPhotoFileUri(photoFileName + "_resized");
            File resizedFile = new File(resizedUri.getPath());

// Write the bytes of the bitmap to file
            try{
                resizedFile.createNewFile();
                FileOutputStream fos = new FileOutputStream(resizedFile);
                fos.write(bytes.toByteArray());
                fos.close();
            }catch (IOException e){
                System.out.println("Error occured");
            }


            imageview.setImageBitmap(rawTakenImage);
        }

    }

        public Uri getPhotoFileUri(String fileName) {
        // Only continue if the SD Card is mounted
            if (isExternalStorageAvailable()) {
            // Get safe storage directory for photos
            // Use `getExternalFilesDir` on Context to access package-specific directories.
            // This way, we don't need to request external read/write runtime permissions.
                File mediaStorageDir = new File(
                    getExternalFilesDir(Environment.DIRECTORY_PICTURES), APP_TAG);

            // Create the storage directory if it does not exist
            if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()){
                Log.d(APP_TAG, "failed to create directory");
            }

            // Return the file target for the photo based on filename
            return Uri.fromFile(new File(mediaStorageDir.getPath() + File.separator + fileName));
        }
        return null;
    }

    // Returns true if external storage for photos is available
        private boolean isExternalStorageAvailable() {
            String state = Environment.getExternalStorageState();
            return state.equals(Environment.MEDIA_MOUNTED);
        }

1 个答案:

答案 0 :(得分:1)

您可以使用以下方式存储图像。

<强> 1。使用Base64的数据库

  • 您可以在数据库中将图片转换为 base64字符串商店。 因此,当您打开应用程序时,您可以在<{1}}中检索 base64 字符串数据库显示图像。

<强> 2。将图像路径存储在数据库中

  • 您可以在数据库中存储图片路径,当您打开应用时,只需检索图片路径和< ImageView中的strong>显示图片。 但是如果从内存中删除图像,则无法从iamge路径获取图像。

第3。将图像存储在服务器中。

  • 如果您存储图像服务器,则可以使用 ImageView 下载图像强>或者是第三方的自由派。并在AsyncTask中显示图片。 (图书馆:Picaso,LazyLoading等)