相关疑难解决方法(0)

从内部存储创建和共享文件

我的目标是在内部存储上创建XML文件,然后通过共享Intent发送它.

我可以使用此代码创建XML文件

FileOutputStream outputStream = context.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
PrintStream printStream = new PrintStream(outputStream);
String xml = this.writeXml(); // get XML here
printStream.println(xml);
printStream.close();
Run Code Online (Sandbox Code Playgroud)

我试图将Uri检索到输出文件以便共享它.我首先尝试通过将文件转换为Uri来访问该文件

File outFile = context.getFileStreamPath(fileName);
return Uri.fromFile(outFile);
Run Code Online (Sandbox Code Playgroud)

这将返回file:///data/data/com.my.package/files/myfile.xml但我似乎无法将其附加到电子邮件,上传等.

如果我手动检查文件长度,它是正确的,并显示有一个合理的文件大小.

接下来,我创建了一个内容提供程序并尝试引用该文件,它不是该文件的有效句柄.在ContentProvider没有以往任何时候都似乎被称为任意点.

Uri uri = Uri.parse("content://" + CachedFileProvider.AUTHORITY + "/" + fileName);
return uri;
Run Code Online (Sandbox Code Playgroud)

这将返回内容://com.my.package.provider/myfile.xml但我检查文件并且它的长度为零.

如何正确访问文件?我是否需要使用内容提供商创建文件?如果是这样,怎么样?

更新

这是我用来分享的代码.如果我选择Gmail,它会显示为附件,但是当我发送它时会出现错误无法显示附件,并且到达的电子邮件没有附件.

public void onClick(View view) {
    Log.d(TAG, "onClick " + view.getId());

    switch (view.getId()) {
        case R.id.share_cancel:
            setResult(RESULT_CANCELED, getIntent());
            finish();
            break;

        case R.id.share_share:

            MyXml …
Run Code Online (Sandbox Code Playgroud)

android android-contentprovider android-file

41
推荐指数
3
解决办法
4万
查看次数

使用ACTION_VIEW在缓存目录中打开文件

我一直在寻找这个,但我无法让它正常工作.让我解释.

我有一个android应用程序,它将文件(图像,文档,...)保存在缓存目录中.起初我习惯于将getExternalCacheDir()方法保存在那里,但因为它应该缓存在没有SD卡的设备上,我必须使用getCacheDir().

当我习惯使用getExternalCacheDir()方法时,在另一个应用程序中打开这些文件是没有问题的,如下所示:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), mimetype);
Run Code Online (Sandbox Code Playgroud)

但是在使用时getCacheDir(),这些文件将保存在应用程序沙箱中,并且无法从应用程序外部访问.所以我用谷歌搜索了它,然后来了**ContentProvider.ContentProvider可以使用外部应用程序打开私有文件.但是当我尝试实现它时,它不起作用.

由于这篇文章,我尝试实现ContentProvider:如何使用Intent.ACTION_VIEW打开保存到内部存储的私有文件?但没有成功.

package com.myapplication.providers;

import java.io.File;
import java.io.FileNotFoundException;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;

public class FileProvider extends ContentProvider {

    @Override
    public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
        File privateFile = new File(getContext().getCacheDir(), uri.getPath());
        return ParcelFileDescriptor.open(privateFile, ParcelFileDescriptor.MODE_READ_ONLY);
    }

    @Override
    public int delete(Uri arg0, String arg1, String[] arg2) {
        return …
Run Code Online (Sandbox Code Playgroud)

android caching android-intent

9
推荐指数
1
解决办法
7688
查看次数

打开图像,意图来自内部存储

我想在Nexus 7平板电脑上使用Android默认图像查看器打开内部文件夹中的图像.我使用以下代码,但由于某种原因,图像不会显示.我做错了什么?该文件的路径是:

file:///data/data/com.example.denandroidapp/files/Attachments/photoTemp/photo.jpg
Run Code Online (Sandbox Code Playgroud)

(这是Uri.parse("file://"+ file)返回的内容).

ArticlePhoto photo =  new ArticlePhoto(soapObject);
File f = new File(context.getFilesDir() + "/Attachments/photoTemp");

if(!f.exists())
    f.mkdirs();

if (photo.ArtPhoto != null) {
    Bitmap articlePhoto = BitmapFactory.decodeByteArray(photo.ArtPhoto, 0, photo.ArtPhoto.length);                      
    ByteArrayOutputStream  bytesFile  =  new ByteArrayOutputStream();
    articlePhoto.compress(Bitmap.CompressFormat.JPEG, 100, bytesFile);

    File file = new File(f + "/photo.jpeg");

    try {
        if(!file.exists())
            file.createNewFile();

        FileOutputStream outStream =  new FileOutputStream(file);

        outStream.write(bytesFile.toByteArray());                  
        outStream.close();

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse("file://" + file),"image/jpeg"); 
        startActivity(intent);

    } catch(Exception ex) {
        AlertDialog alert =  new  AlertDialog.Builder(context).create();
        alert.setTitle("Warning!");
        alert.setMessage(ex.getMessage());
        alert.show();
    }
}
Run Code Online (Sandbox Code Playgroud)

android

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