簡體   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