繁体   English   中英

将PDF文件从资产复制到SD卡

[英]copying pdf file from assest to sd card

我已经建立了从assests文件夹打开pdf文件的应用程序,它可以正常工作,但该文件是在内部存储中创建的。如何为用户隐藏它。 有没有一种方法可以将其存储在根文件夹中,以便用户看不到它。

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    File fileBrochure = new File(Environment.getExternalStorageDirectory()+ "/" + "abc.pdf");




    if (!fileBrochure.exists())
    {
        Toast.makeText(this, "copying", Toast.LENGTH_SHORT).show();

        CopyAssetsbrochure();
    }

    /** PDF reader code */
    File file = new File(Environment.getExternalStorageDirectory()+ "/" + "abc.pdf");

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file),"application/pdf");

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try
    {

        getApplicationContext().startActivity(intent);

    }
    catch (ActivityNotFoundException e)
    {
        Toast.makeText(this, "NO Pdf Viewer", Toast.LENGTH_SHORT).show();
    }
}

private void CopyAssetsbrochure() {
    AssetManager assetManager = getAssets();
    String[] files = null;
    try
    {
        files = assetManager.list("");
    }
    catch (IOException e)
    {
        Log.e("tag", e.getMessage());
    }

    for(int i=0; i<files.length; i++)
    {
        String fStr = files[i];
        if(fStr.equalsIgnoreCase("abc.pdf"))
        {
            InputStream in = null;
            OutputStream out = null;
            try
            {
                in = assetManager.open(files[i]);
                out = new FileOutputStream(Environment.getExternalStorageDirectory()+ "/" + files[i]);
                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
                break;
            }
            catch(Exception e)
            {
                Log.e("tag", e.getMessage());
            }
        }
    }
}

private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
        out.write(buffer, 0, read);
    }
}

不可能。 您无法访问root。

您可以通过使用加密(即加密和解密)来满足您的要求。

加密文件,然后将其存储在内部存储中,同时在应用程序内部进行读取时将其解密。 你会做的。

如果有任何入侵者从内部存储中复制您的文件,则他或她将无法解密该文件。

暂无
暂无

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

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