【问题标题】:Saving an image file is too slow to immediately show it in an Imageview保存图像文件太慢,无法立即在 Imageview 中显示
【发布时间】:2017-01-16 05:34:05
【问题描述】:

在我的应用程序中,我保存了一个图像文件(从照相手机拍摄的照片),但如果我想立即在 Imageview 中显示其缩略图,有时会出现错误。为什么?保存过程是否太慢?我可以使用替代方法来保存我的图像文件吗?

我在这里调用我的相机:

        camera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (intent.resolveActivity(getActivity().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
                    Log.v("Crea file Immagine", "IOException");
                }
                // Continue only if the File was successfully created
                if (photoFile != null) {
                    Log.v("photofile",String.valueOf(photoFile));
                    Uri mSelectedImageUri= Uri.fromFile(photoFile);
                    Log.v("mSelectedImageUri", String.valueOf(mSelectedImageUri));
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, mSelectedImageUri);
                    startActivityForResult(intent, CAMERA_REQUEST);
                }
            }
        }
    });

我在这里保存我的图像文件:

    private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    sharedPreference.save(context,"nomeFile",timeStamp);

    File storageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DCIM) + File.separator + "BottiglieDiVino");
    boolean success;
    if (!storageDir.exists()) {
        success = storageDir.mkdir();
        if (success) {
            // Do something on success
            Log.v("Crea /BottiglieDiVino", "Ho Creato nuova directory");
        } else {
            // Do something else on failure
            Log.v("Crea /BottiglieDiVino", "ERRORE nel creare nuova directory");
        }
    }

    File image = new File(storageDir, timeStamp + ".jpg");
    // Save a file: path for use with ACTION_VIEW intents
    percorso = image.getAbsolutePath();
    sharedPreference.save(context,"percorso", percorso);
    Log.v("percorso", percorso);
    return image;
}

在这里我得到了这个错误

java.io.FileNotFoundException: /: open failed: EISDIR (Is a directory)
libcore.io.IoBridge.open(IoBridge.java:409)
java.io.FileInputStream.<init>(FileInputStream.java:78)
java.io.FileInputStream.<init>(FileInputStream.java:105)
android.content.ContentResolver.openInputStream(ContentResolver.java:630) 

com.example.android.swipetabs.FragmentTab2.decodeSampledBitmapFromUri(FragmentTab2.java:

附言在最后一行:) 是错误。

    @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {

        percorso = sharedPreference.getValue(context, "percorso");

        sharedPreference.save(context,"check23", "ok");
        Log.v("percorso", percorso);
        foto.setImageBitmap(display(percorso));

    }
}

    public Bitmap decodeSampledBitmapFromUri(Uri uri, int reqWidth, int reqHeight) {

    try {
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri), null, options);

请帮帮我。

【问题讨论】:

    标签: android image file thumbnails android-camera-intent


    【解决方案1】:

    注意:为 marashmallow 或更高版本提供这些权限运行时间,并在您的其他设备上获得权限时放置您的代码,它可以正常工作

    在主祭中

     <uses-permission android:name="android.permission.CAMERA" />
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    运行时

     public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 10;
    
    camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
            permissions= new String[]{
                        Manifest.permission.CAMERA,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE};
    
    
                if (checkPermissions())
                {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    if (intent.resolveActivity(demo.this.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
                            Log.v("Crea file Immagine", "IOException");
                        }
                        // Continue only if the File was successfully created
                        if (photoFile != null) {
                            Log.v("photofile",String.valueOf(photoFile));
                            Uri mSelectedImageUri= Uri.fromFile(photoFile);
                            Log.v("mSelectedImageUri", String.valueOf(mSelectedImageUri));
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, mSelectedImageUri);
                            startActivityForResult(intent, CAMERA_REQUEST);
                        }
                    }
    
    
    
                }
    
    
            }
    
        });
    
     private  boolean checkPermissions() {
        int result;
        List<String> listPermissionsNeeded = new ArrayList<>();
        for (String p:permissions) {
            result = ContextCompat.checkSelfPermission(this,p);
            if (result != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(p);
            }
        }
        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),REQUEST_ID_MULTIPLE_PERMISSIONS);
            return false;
        }
        return true;
    }       
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-07
      • 2016-11-05
      • 1970-01-01
      • 2021-03-21
      • 2014-06-02
      • 1970-01-01
      • 2016-04-03
      • 1970-01-01
      相关资源
      最近更新 更多