Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.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 - Fatal编程技术网

在android应用程序中,图像不是';关闭应用程序并重新启动后,屏幕上不会显示

在android应用程序中,图像不是';关闭应用程序并重新启动后,屏幕上不会显示,android,Android,在一个应用程序中,我允许用户从图库中选择图像,或者他可以从相机中选择。虽然我可以第一次管理图像并在活动中显示,但在关闭应用程序并重新启动后,图像消失,空间空白。有人向我解释将图像数据保存在SharedReferences中,但我是android新手,不太了解。我寻找共享的参考资料,但不知道如何让它工作。 因此,如果有人能提供一些解释和代码,这将对我有很大帮助。 谢谢 这就是我试图做的 private void openCamera(){ // create Intent

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

    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。使用Base64的数据库

  • 您可以将图像转换为base64字符串存储在数据库中。 因此,当您打开应用程序时,您可以数据库中检索base64字符串,并
    图像视图中显示图像
    
2。在数据库中存储图像路径

  • 您可以在数据库中存储图像路径,打开应用程序时,只需检索图像路径,并
    图像视图
    中显示图像。 但若从内存中删除图像,则无法从iamge路径中获取图像
3。将图像存储在服务器中。

  • 如果将图像存储在服务器中,则可以使用
    AsyncTask
    或sime第三方文库检索图像路径并下载图像。并在
    ImageView
    中显示图像。 (自由主义者:皮卡索、懒汉等)

图像数据太大,如果您正在学习,请不要将图像数据保存在
SharedReference
中。在数据库中保存图像base64或在数据库中保存图像路径以查看图像。非常感谢。我还没有学会数据库。所以我想我应该开始了。你应该。你可以在网上找到任何东西。有没有免费的SQL Server可以推荐?