图像不会在ImageView中显示(没有这样的文件或目录)

时间:2017-06-04 07:26:55

标签: android

我正在编写一个应用程序,用户可以点按按钮创建数字签名。完成后,用户点击保存并保存图像。该文件夹确实创建,图像确实保存(我可以浏览到我手机上的文件夹,可以看到它创建并保存图像)但是当试图显示图像时,它表示"没有这样的fie或目录存在"

我拥有读取和写入外部存储的正确权限。

以下是代码:

String DIRECTORY = Environment.getExternalStorageDirectory().getPath() + 
"/DigitSign/";
String pic_name = new SimpleDateFormat("yyyyMMdd_HHmmss", 
Locale.getDefault()).format(new Date());
String StoredPath = DIRECTORY + pic_name + ".png";


@TargetApi(Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dateFormatter = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
    verifyStoragePermissions(this);

    File folder = new 
    File(Environment.getExternalStorageDirectory().getPath() + "/DigitSign/");
    allFiles = folder.listFiles();

   .........

  btn_get_sign = (Button) findViewById(R.id.SignatureButton);
    ImageView custsig = (ImageView)findViewById(R.id.custimagesig);

    // Method to create Directory, if the Directory doesn't exists
    file = new File(DIRECTORY);
    if (!file.exists()) {
        file.mkdir();
    }
    if(file.exists()){

        File sd = Environment.getExternalStorageDirectory();
        File image = new File(sd+DIRECTORY, StoredPath);
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
        custsig.setImageBitmap(bitmap);

    }

    // Dialog Function
    dialog = new Dialog(MainActivity.this);
    // Removing the features of Normal Dialogs
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.signature);
    dialog.setCancelable(true);

    btn_get_sign.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MediaScannerConnection.scanFile(
                    getApplicationContext(),
                    new String[]{file.getAbsolutePath()},
                    null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        @Override
                        public void onScanCompleted(String path, Uri uri) {
                            Log.v("digiscan",
                                    "file " + path + " was scanned successfully: " + uri);
                        }
                    });

            dialog_action();

        }
    });
}
// Function for Digital Signature
public void dialog_action() {

    mContent = (LinearLayout) dialog.findViewById(R.id.linearLayout);
    mSignature = new signature(getApplicationContext(), null);
    mSignature.setBackgroundColor(Color.WHITE);
    // Dynamically generating Layout through java code
    mContent.addView(mSignature, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    mClear = (Button) dialog.findViewById(R.id.clear);
    mGetSign = (Button) dialog.findViewById(R.id.getsign);
    mGetSign.setEnabled(false);
    mCancel = (Button) dialog.findViewById(R.id.cancel);
    view = mContent;

    mClear.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.v("tag", "Panel Cleared");
            mSignature.clear();
            mGetSign.setEnabled(false);
        }
    });
    mGetSign.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            Log.v("tag", "Panel Saved");
            view.setDrawingCacheEnabled(true);
            mSignature.save(view, StoredPath);
            dialog.dismiss();
            Toast.makeText(getApplicationContext(), "Successfully Saved", Toast.LENGTH_SHORT).show();
            // Calling the same class
            recreate();
        }
    });
    mCancel.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.v("tag", "Panel Cancelled");
            dialog.dismiss();
            // Calling the same class
            recreate();
        }
    });
    dialog.show();
}

老实说,我不知道为什么会这样? 有人可以帮我解决这个问题吗?

0 个答案:

没有答案