Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.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
Java 将图像保存到文件夹,但显示零字节大小,检索以显示图像时不显示任何内容_Java_Android_Debugging - Fatal编程技术网

Java 将图像保存到文件夹,但显示零字节大小,检索以显示图像时不显示任何内容

Java 将图像保存到文件夹,但显示零字节大小,检索以显示图像时不显示任何内容,java,android,debugging,Java,Android,Debugging,权限: <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permissio

权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />`

请提供您请求的操作系统版本、SDK版本(minSdk和TargetSDK)和权限。我将添加日志语句,以便在您重新计算photoWidth或targetW时使用。。。。这样,您就可以找出意外值的实际位置。可能是错误地返回了0。看起来您没有请求运行时权限。请提供操作系统版本、SDK版本(minSdk和TargetSDK)以及您请求的权限。无论何时重新计算photoWidth或targetW,我都会添加日志语句。。。。这样,您就可以找出意外值的实际位置。可能是错误地返回了0。看起来您没有请求运行时权限。
        public class CameraScan extends AppCompatActivity {
            private ImageView imageHolder;
            String mCurrentPhotoPath;
            android.support.v7.app.ActionBar actionBar;
            static final int REQUEST_IMAGE_CAPTURE = 1;
            static final int REQUEST_TAKE_PHOTO = 1;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_camera_scan);
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                setSupportActionBar(toolbar);
                actionBar = getSupportActionBar();
                // TODO: Remove the redundant calls to getSupportActionBar()
                // and use variable actionBar instead
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                getSupportActionBar().setHomeButtonEnabled(true);
                imageHolder = (ImageView) findViewById(R.id.captured_photo);
                Button capturedImageButton = (Button) findViewById(R.id.photo_button);
                if(isSdPresent()==true)
                {
                   capturedImageButton.setOnClickListener(new Button.OnClickListener()
                    {
                        @Override
                        public void onClick(View v)
                        {
                            dispatchTakePictureIntent();
                        }
                    });
                }
                else
                {
                    Toast.makeText(this,"Insert SDCard", Toast.LENGTH_LONG).show();
                }

            }
          //get the thumbnail
            @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

                    Bundle extras = data.getExtras();
                    Bitmap imageBitmap = (Bitmap) extras.get("data");
                    imageHolder.setImageBitmap(imageBitmap);
                }
            }
            //save fullsize photo
            private File createImageFile() throws IOException {
                // Create an image file name
                String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                String imageFileName = "KAWACH_" + 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
                mCurrentPhotoPath = image.getAbsolutePath();

                setPic();
                galleryAddPic();
                return image;
            }
            //take pic wd camera app
            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.darklord.kawach",
                                photoFile);
                        Bundle data = new Bundle();
                        data.putString(MediaStore.EXTRA_OUTPUT, photoURI.toString());
                        //takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                        startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
                    }
                }
            }
            //adding to gallery
            private void galleryAddPic() {
                Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                File f = new File(mCurrentPhotoPath);
                Uri contentUri = Uri.fromFile(f);
                mediaScanIntent.setData(contentUri);
                this.sendBroadcast(mediaScanIntent);
            }
            //seting image
            private void setPic() {
                // Get the dimensions of the View
                int targetW = imageHolder.getWidth();
                int targetH = imageHolder.getHeight();

                // Get the dimensions of the bitmap
                try {
                    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                    bmOptions.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
                    int photoW = bmOptions.outWidth;
                    int photoH = bmOptions.outHeight;

                    // Determine how much to scale down the image
                    int scaleFactor = Math.min(photoW/targetW,photoH/targetH);

                    // Decode the image file into a Bitmap sized to fill the View
                    bmOptions.inJustDecodeBounds = false;
                    bmOptions.inSampleSize = scaleFactor;
                    bmOptions.inPurgeable = true;

                    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
                    //imageHolder.setImageBitmap(bitmap);
                    if(photoW*photoH > targetW*targetH){
                        int imageSizef = (int)(photoW*photoH/targetW*targetH);
                        String imageSize = imageSizef+"";
                        bmOptions.inSampleSize = Integer.parseInt(imageSize);
                        bmOptions.inJustDecodeBounds = false;
                        imageHolder.setImageBitmap(BitmapFactory.decodeFile(mCurrentPhotoPath,bmOptions));
                    }else{
                        imageHolder.setImageBitmap(BitmapFactory.decodeFile(mCurrentPhotoPath,bmOptions ));
                    }
                }catch (Exception e){
                    Log.d("Error",""+e);
                }
            }
            //sd present or not
            public static boolean isSdPresent()
            {
                return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
            }
            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.camera_bar, menu);
                return true;
            }
            @Override
            public boolean onOptionsItemSelected(MenuItem item) {

                Intent myIntent = new Intent(getApplicationContext(), KawachHome.class);
                startActivityForResult(myIntent, 0);

                switch (item.getItemId()) {
                    case R.id.action_settings:
                        // User chose the "Settings" item, show the app settings UI...
                        return true;

                    case R.id.action_gallery:
                        // User chose the "Favorite" action, mark the current item
                        // as a favorite...
                        Intent i = new Intent(this,Kawach_gallery.class);
                        this.startActivity(i);

                        return true;

                    default:
                        // If we got here, the user's action was not recognized.
                        // Invoke the superclass to handle it.
                        return super.onOptionsItemSelected(item);

                }

            }
        }