有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当我再次尝试上传时,java Android无法查看保存在新目录中的图像

我正在从gallery上传一张图片,并在裁剪后将其保存到新目录中(使用Android image cropper library)

正在创建目录并成功保存图像。 它可以在文件资源管理器中找到,但当我尝试在应用程序中上载相同的图像时,它不会出现

我还尝试将图像保存在现有目录中,该目录在搜索中可见,而不是其中的图像,但可以在文件资源管理器中找到

编辑活动

save_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveImage();
                }
        });
    }

public void saveImage(){
        try {
            InputStream inputStream = getContentResolver().openInputStream(resultUri);
            bitmap = BitmapFactory.decodeStream(inputStream);;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        File file = Environment.getExternalStorageDirectory();
        File dir = new File(file.getAbsolutePath()+"/Pictures/");
        if(!dir.exists()){
            dir.mkdirs();
        }
        File f = new File(dir,System.currentTimeMillis()+".jpg");
        try {
            outputStream = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        bitmap.compress(Bitmap.CompressFormat.JPEG,90,outputStream);
        Toast.makeText(getApplicationContext(),f.toString(),Toast.LENGTH_SHORT).show();
        try {
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

main活动


    private void cam_permission() {
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.CAMERA}, CAMERA_PERMISSION_CODE);
        } else{
            dispatchTakePictureIntent();
        }
    }


    private  void storage_permission(){
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
        } else{
            Intent gallery = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(gallery, GALLERY_REQUEST_CODE);
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        if (requestCode == CAMERA_PERMISSION_CODE) {
            if (grantResults.length > 0 
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            } else {
                Toast.makeText(this, "Camera Permission is required", Toast.LENGTH_SHORT).show();
            }
        }
        else if (requestCode == STORAGE_PERMISSION_CODE) {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            }
            else {
                Toast.makeText(MainActivity.this, "Storage Permission is required", Toast.LENGTH_SHORT).show();
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

        if (requestCode == CAMERA_REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                File file = new File(currentPhotoPath);
                Uri contentUri = Uri.fromFile(file);


                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent .setClass(MainActivity.this,  EditActivity.class);
                intent .putExtra("imageUri", contentUri.toString());
                startActivity(intent);
            }
        }
        if (requestCode == GALLERY_REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                Uri contentUri = data.getData();
                String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent .setClass(MainActivity.this,  EditActivity.class);
                intent .putExtra("imageUri", contentUri.toString());
                startActivity(intent);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        currentPhotoPath = image.getAbsolutePath();
        return image;
    }



    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File

            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.example.安卓.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
            }
        }
    }

AndroidManifest。xml

    <uses-permission 安卓:name="安卓.permission.CAMERA" />
    <uses-permission 安卓:name="安卓.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission 安卓:name="安卓.permission.WRITE_EXTERNAL_STORAGE" />

共 (0) 个答案