简体   繁体   English

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

[英]copying pdf file from assest to sd card

i have built the app that opens pdf file from assests folder ,it is working properly but the file is created in internal storage.How to hide it for the user. 我已经建立了从assests文件夹打开pdf文件的应用程序,它可以正常工作,但该文件是在内部存储中创建的。如何为用户隐藏它。 Is there a way to store it in root folder so user cannot see it . 有没有一种方法可以将其存储在根文件夹中,以便用户看不到它。

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);
    }
}

not possible. 不可能。 you can't get access to root. 您无法访问root。

You can achieve your requirement by making use Cryptography that is Encryption and Decryption. 您可以通过使用加密(即加密和解密)来满足您的要求。

Encrypt the file and store it on internal storage while reading inside the application decrypt it. 加密文件,然后将其存储在内部存储中,同时在应用程序内部进行读取时将其解密。 You will done. 你会做的。

if any intruder copy your file from internal storage then he or she will unable to decrypt it. 如果有任何入侵者从内部存储中复制您的文件,则他或她将无法解密该文件。

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

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