简体   繁体   English

在Android中创建文件路径

[英]Create a filepath in android

Code when I crop an existing image in gallery 裁剪图库中的现有图像时的代码

 final Uri uri = result.getUri();
 StorageReference thumb_filepath = mStorage.child("Raw").child(UID + ".jpg");
 File thumb_filePath = new File(uri.getPath());
 Log.d("DFGHJKFGHJ", "CREATED FILEEEE" + thumb_filePath);

Logcat Result Logcat结果

 02-23 13:51:20.053 11130-11130/com.appmaster.akash.messageplus D/DFGHJKFGHJ: CREATED FILEEEE/data/user/0/com.appmaster.akash.messageplus/cache/cropped9075402641648633426.jpg

Now I used the same method for getting an image from firebase and converting that to uri and then FIle But the output is different 现在,我使用了相同的方法从Firebase获取图像并将其转换为uri,然后转换为文件,但是输出是不同的

Code

Uri uri = null;
uri = Uri.parse(String.valueOf(url));
Log.d("DFGHJKFGHJ", "CREATED URI : " + uri);

File thumb_filePath = new File(uri.getPath());
Log.d("DFGHJKFGHJ", "CREATED FILEEEE" + thumb_filePath);

URI is getting created but The output of file is 正在创建URI,但文件输出为

02-23 13:50:48.326 11130-11130/com.appmaster.akash.messageplus D/DFGHJKFGHJ: CREATED FILEEEE/v0/b/messageplus-cd647.appspot.com/o/hackerpic.jpg

Because of this the next feature is also not working. 因此,下一个功能也不起作用。 SO can anyone help me get the output like the first one for firebase image as well please? 所以有人可以帮我获得类似于Firebase图像的第一个输出吗?

Download image from firebase storage and save locally: 从Firebase存储下载图像并保存在本地:

private void downloadImgFromFStorageToLocalFile(StorageReference fileRef) {
    if (fileRef != null) {
        progressDialog.setTitle("Downloading...");
        progressDialog.setMessage(null);
        progressDialog.show();

        try {
            final File localFile = File.createTempFile("images", "jpg");

            fileRef.getFile(localFile).addOnSuccessListener(new 
                OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(FileDownloadTask.TaskSnapshot 
                 taskSnapshot) {
                    Bitmap bmp = 
                    BitmapFactory.decodeFile(localFile.getAbsolutePath());
                    imgViewProfile.setImageBitmap(bmp);
                    progressDialog.dismiss();
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    progressDialog.dismiss();
                    Toast.makeText(MainActivity.this, exception.getMessage(), 
                    Toast.LENGTH_LONG).show();
                }
            }).addOnProgressListener(new 
                OnProgressListener<FileDownloadTask.TaskSnapshot>() {
                @Override
                public void onProgress(FileDownloadTask.TaskSnapshot 
                 taskSnapshot) {
                    // progress percentage
                    double progress = (100.0 * 
                      taskSnapshot.getBytesTransferred()) / 
                      taskSnapshot.getTotalByteCount();
                    // percentage in progress dialog
                    progressDialog.setMessage("Downloaded " + ((int) progress) 
                                                            + "%...");
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        Toast.makeText(MainActivity.this, "File ref is null", 
             Toast.LENGTH_LONG).show();
    }
}

Create your own path and save the file 创建自己的路径并保存文件

    String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Folder_name";

    File folder = new File(path);
    if (!folder.exists()) {
        folder.mkdir();
    }


 Uri uri = Uri.fromFile(folder);
 File thumb_filePath = new File(uri.toString());
 assertEquals(folder.getAbsolutePath(), thumb_filePath .getAbsolutePath());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM