标签: android-file

如何从txt删除行?

我的意思是,我想从android上的文本中删除一行.我怎么删除?我不想读取一个txt并使用删除行创建另一个.我想删除现有txt中的行.谢谢.

java android fileoutputstream android-file

2
推荐指数
1
解决办法
4193
查看次数

同一文件上的多个FileObserver失败

在我的应用程序中,我有不同的组件,它们使用FileObservers监视sdcard中的特定文件.因此,有两个文件观察器实例观察单个文件,例如所有事件的abc.xml.

FileObserver fo1 = new FileObserver(new File("/sdcard/abc.xml"));
fo1.startWatching();
FileObserver fo2 = new FileObserver(new File("/sdcard/abc.xml"));
fo2.startWatching();
Run Code Online (Sandbox Code Playgroud)

他们都注册了不同的活动.我的问题是当两个文件观察者并行观察时,我错过了对"fo1"的onEvent()的调用.

这是Android系统的限制吗?有什么方法可以解决这个问题?

android android-file fileobserver

2
推荐指数
1
解决办法
975
查看次数

如何从Android中的图片库中删除文件?

我正在制作一个具有允许将图像上传到Web服务器的功能的应用程序。将捕获的图像上传到服务器后,该图像将从用户的设备中删除。我可以上传图像,并在检查文件管理器后将其删除。但是当我打开图库时,尽管它显示“无法加载文件”,但它仍然存在。但是,当我检查详细信息时,它会显示图像的大小。那么,如何从文件管理器和图库中完全删除上传的图像?

这是我的代码:

Public Static Boolean deleteDirectory(File path) {
        // TODO Auto-generated method stub
        if( path.exists() ) {
            File files = path;

                if(files.isDirectory()) {
                    deleteDirectory(files);
                }
                else {
                    files.delete();
                }
            }

        return(path.delete());
    }
Run Code Online (Sandbox Code Playgroud)

android image delete-file android-contentprovider android-file

2
推荐指数
1
解决办法
5983
查看次数

如何将 PdfDocument 对象保存到 Android 中的文件中?

在我的应用程序中,我在按钮单击事件上创建了一个 PDF 文档。并且我使用了打印框架来打印该文档。所有这些都工作正常。我需要的是,我想将该 pdf 文档保存到手机存储中的文件夹中.如何做到这一点,我不知道如何将此 pdf 保存到文件中。有人可以帮我吗?提前谢谢。

我的生成pdf的代码

public class CustomPrintActivity extends Activity {





@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_custom_print);
}



public void printDocument(View view)
{
    PrintManager printManager = (PrintManager) this
            .getSystemService(Context.PRINT_SERVICE);

    String jobName = this.getString(R.string.app_name) +
            " Document";

    printManager.print(jobName, new MyPrintDocumentAdapter(this),
            null);
}
public class MyPrintDocumentAdapter extends PrintDocumentAdapter {
    Context context;
    private int pageHeight;
    private int pageWidth;
    public PdfDocument myPdfDocument;
    public int totalpages = 1;


    public MyPrintDocumentAdapter(Context context) {
        this.context = context;
    }

    @Override
    public void onLayout(PrintAttributes …
Run Code Online (Sandbox Code Playgroud)

android android-file android-print-framework android-pdf-api

2
推荐指数
1
解决办法
4973
查看次数

Android 在外部 SD 卡上写入文件 for android 5+

我无法将文件写入外部 SD 卡。我收到错误消息 EAcess denied。我在互联网上搜索了很多,发现从 Android 4.4 + android 的存储访问框架 (SAF) 写入文件是必需的。

但我正在使用一些能够在 SD 卡上写入(创建/删除/重命名)文件的 android 应用程序。他们没有使用 SAF。

因此,请帮助我了解如何在不使用 SAF 框架的情况下执行此操作。

谢谢

file-io android android-sdcard android-file

2
推荐指数
1
解决办法
6297
查看次数

如何从Android内部存储中读取文件名

我想从我的应用程序的默认内部存储文件夹中读取文件名,但即使有一些文件,我也会得到一个大小为 0 的列表,我使用以下内容来读取文件名

File dirFiles = Settings.this.getFilesDir();
    File list[] = dirFiles.listFiles();
    for( int i=0; i< list.length; i++)
    {
        myList.add( list[i].getName() );
    }
Run Code Online (Sandbox Code Playgroud)

我在这里做错了什么?我得到的路径正确吗?或者它需要在它的末尾有一个“/”?

/data/data/com.compfrind.contacts/files
Run Code Online (Sandbox Code Playgroud)

android internal android-file

2
推荐指数
1
解决办法
2446
查看次数

在Android中保存文件的正确路径

我正在尝试为 Android 编写一个应用程序,我需要从服务器保存 resposne,我设法通过将其保存在此路径中的 txt 文件中来做到这一点:

String path= "/storage/emulated/0/Android/data/com.example.simone.pizzino/files/response.txt"; final File file = new File(path);

在我朋友的手机上测试,他看不到那个路径下的文件夹,他的路径类似于data/data/"packageName",它也不适用于 Android Studio 中的模拟器。我的手机是运行 7.1 库存 rom 的 Nexus 5X。我的朋友使用 6.1 扎根。有没有办法获得应用程序文件夹的动态路径而不必将其声明为常量?

抱歉,如果已经问过这个问题,但我找不到解决我的问题的方法。

android android-file

2
推荐指数
1
解决办法
1万
查看次数

如何在 Android 的内部缓存目录中创建图像文件

我想将 image.png 放在 Android 内部存储的 cache/images/ 中。

我无法使用以下代码获得它:

File directory = new File(getContext().getCacheDir(), "images");
directory.mkdirs();


File mypath=new File(directory,"image.png");

FileOutputStream fos = null;
try {
    fos = new FileOutputStream(mypath);

    bmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}       
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,我什至无法创建名为 images 的目录。请帮助,我是初学者。

android android-file

2
推荐指数
1
解决办法
8743
查看次数

打开失败:EACCES(拒绝权限)api23?

我知道运行时权限,并且已经考虑了这一点,但是它仍然无法正常工作。我添加了以下几行来体现:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)

这是我的onCreate方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new);
    if (shouldAskPermissions()) {
        askPermissions();
    }
    generateNoteOnSD(this,fileName,msg);
    readFile(fileName);
}
Run Code Online (Sandbox Code Playgroud)

这是用于请求和授予许可

protected static boolean shouldAskPermissions() {
    return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
}

@TargetApi(23)
protected void askPermissions() {
    String[] permissions = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE"
    };
  /*  int requestCode = 200;
    requestPermissions(permissions, requestCode);*/
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so …
Run Code Online (Sandbox Code Playgroud)

android android-manifest android-file android-permissions runtime-permissions

2
推荐指数
1
解决办法
3047
查看次数

无法从Android中的MI-Oreo(MI-A2)设备的下载文件夹中获取用于选择任何文件的URI

我需要帮助来获取我们通过 Intent 从下载文件夹或任何其他文件夹中选择的内容的文件路径。我附上了一个我一直在使用的fileUtil类,它在牛轧糖版本之前运行良好。但是对于奥利奥以及在某些特定设备中也是如此,我们会收到一个空路径作为回报。因此,如果有人遇到过此类错误并找到了解决方案,请在此处分享。

if(resultCode ==RESULT_OK)
{
    final Uri uri = data.getData();
    // Get the File path from the Uri
    String path = FileUtils.getPath(this, uri);
    // Alternatively, use FileUtils.getFile(Context, Uri)
    if (path != null && FileUtils.isLocal(path)) {
        //File file = new File(path);
        this.file = new File(path);
        profile_imagepath = path;
        filename = file.getName();
        txtselectedfile.setText(filename);
    }
}
Run Code Online (Sandbox Code Playgroud)

uri file android-file android-fileprovider

2
推荐指数
1
解决办法
1805
查看次数