Store pdf from assets to internal storage(private) - android

In my applications i am having few pdf stored in my assests folder.Now what i want is to copy those pdfs to interal storage (private) asin (com.android.mypackage).I have written the code but seems not working.I am getting data for path not found
Code
private void CopyReadAssets() {
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = getDir("Mcq Demo", Context.MODE_PRIVATE);
try {
in = assetManager.open("Geography1.pdf");
out = new BufferedOutputStream(new FileOutputStream(file));
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
Intent intent = new Intent(Intent.ACTION_VIEW);
File mcqDemo = new File(file, "Geography1.pdf");
intent.setDataAndType(
Uri.parse("file://" + mcqDemo),
"application/pdf");
startActivity(intent);
}
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);
}
}

First, you are trying to write a PDF to a directory. That will not work. If you want to write a PDF to a file named Geography1.pdf, you need to do so explicitly, with a FileOutputStream pointing to such a file.
Second, your Intent will not work, as third-party apps have no access to your internal storage.

Code for Copy File from assets to directory and Open file
Variables
public static final String FILE_NAME = "my_file_name";
public final static String FILE_PATH = data/data/Your_Package_Name/";
Method
private void copyFileFromAssets() throws IOException {
String outFileName = FILE_PATH + FILE_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
InputStream myInput = myContext.getAssets().open(FILE_NAME);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myInput.close();
myOutput.flush();
myOutput.close();
}
To open PDF file
Intent intent = new Intent(Intent.ACTION_VIEW);
File mcqDemo = new File(outFileName);
intent.setDataAndType(Uri.fromFile(mcqDemo), "application/pdf");
startActivity(intent);
Done

Related

Store db file in android app

I've implemented a parser that parses web-site and then i get a big object that consists of a lot of other objects and arrays and etc. What i want is i need to store all this data to SQLite database, then export the .db file.
Then i want to create an app which will work with that db file. How can i implement it in android?
can i store this db-file to assets and then replace the real db file of the app with this db-file that i get from assets?
Of course i can use json or xml or whatever, but it will take a lot of time to unwrap this data for the final user of the application.
So the idea is to generate the db-file once and then store in the assets.
So i've ended up with a solution. Here is a method to get the sqlite db file from your project
public void backupDB(){
// getDatabasePath
final String inFileName = "/data/data/com.example.yourproject/databases/books-db";
File dbFile = new File(inFileName);
try {
FileInputStream fis = new FileInputStream(dbFile);
String outFileName = Environment.getExternalStorageDirectory()+"/database_copy.db";
// Open the empty db as the output stream
OutputStream output = new FileOutputStream(outFileName);
// Transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer))>0){
output.write(buffer, 0, length);
}
// Close the streams
output.flush();
output.close();
fis.close();
} catch (FileNotFoundException fileNotFoundException){
Log.e(LOG_TAG,"fileNotFoundException");
fileNotFoundException.printStackTrace();
}catch (IOException ioException){
Log.e(LOG_TAG,"ioException");
ioException.printStackTrace();
}
}
here is method to get it from assets to replace the db of other project, where you want to use this database instead of other.
private void copyAssets() {
AssetManager assetManager = mContext.getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open("database_copy.db");
String currentDBPath = "/data/data/com.example.somepackagename/databases/books-db";
out = new FileOutputStream(currentDBPath);
copyFile(in, out);
in.close();
out.flush();
out.close();
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file " , e);
}
}
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);
}
}

how can i copy file in data directory

i want to copy file from asset to other application data directory.i have also given root acess to it.but this code doesn't work.its work on extenal storage directory .but not copy file in data directory..
public void onClick(View arg1){
String command[] = { "su", "-c", "ls", "/data" };
Shell shell = new Shell();
String text = shell.sendShellCommand(command);
if (new File((Object)Environment.getDataDirectory() + "/data/com.my/shared_pref/com.myxml").exists()) {
Toast.makeText(getApplicationContext(),"copied",Toast.LENGTH_LONG).show();
MainActivity.this.copyAssets();
}
else{
Toast.makeText(getApplicationContext(),"error! copy failed ",Toast.LENGTH_LONG).show();
private void copyAssets()
{
AssetManager assetManager = getAssets();
String[] files = null;
InputStream in = null;
OutputStream out = null;
String filename =
"com.my.xml" ;
try
{
in = assetManager.open( filename);
out = new FileOutputStream((Environment.getDataDirectory().toString() +"/data/com.my/shared_pref/" + filename));
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
}
catch (IOException e)
{
Log. e ( "tag" , "Failed to copy asset file: " , e);
}
}
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);
}
}
if anyone knows how to do it?
Pleaase suggest me.!!
Thanks.!!
This is exactly what you need. This code copies sample.apk from assets to the application data directory. You can change the path to anywhere you want.
private String copyAssets() {
AssetManager assetManager = getActivity().getAssets();
InputStream in = null;
OutputStream out = null;
String filename = "sample.apk";
String path = Environment.getExternalStorageDirectory()
+ "/Android/data/"
+ getActivity().getPackageName()
+ "/files";
try {
in = assetManager.open("files/" + filename);
File outFile = new File(path);
if (!outFile.exists()) {
outFile.mkdirs();
}
out = new FileOutputStream(outFile + "/" + filename);
copyFile(in, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return path + "/" + filename;
}
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);
}
}
Use this code. I used this code in my project, Got it on net somewhare, it works.
private void copyDataBase() throws IOException
{
//Open your local db as the input stream
InputStream myInput = _context.getAssets().open(DB_NAME);
// Path to db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0)
{
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}

Trying to read pdf file stored in res folder android

Here is code given below..wenever i try to open dat activity it shows the target file doesnt exist..please help me.....thanks in advance
public class MainActivityAlgb extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_algb);
CopyReadAssets();
}
private void CopyReadAssets()
{
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "cure.pdf");
try
{
in = assetManager.open("cure.pdf");
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e)
{
Log.e("tag", e.getMessage());
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + getFilesDir() + "/cure.pdf"), "application/pdf");
startActivity(intent);
}
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);
}
}
}
As per my understanding you have stored the file in res folder.I assume that it is inside raw folder, if not move it to raw folder.Then you can get the file input stream as
InputStream is = getResources().openRawResource(R.raw.yourFile);
Hope this helps.

How to open PDF file in Android from the assets folder?

Does any one has an idea on how to open a PDF file in Android? My code looks this this:
public class SampleActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
CopyReadAssets();
}
private void CopyReadAssets() {
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "git.pdf");
try {
in = assetManager.open("git.pdf");
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/git.pdf"),
"application/pdf");
startActivity(intent);
}
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);
}
}
}
Here is the code for opening pdf file from asset folder, but you must have pdf reader installed on your device :
private void CopyAssets() {
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "fileName.pdf");
try {
in = assetManager.open("fileName.pdf");
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/fileName.pdf"),
"application/pdf");
startActivity(intent);
}
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);
}
}

Copying a file to sdcard

I'm desperately trying to copy a file to the sdcard from my raw folder, but it won't work! The file just doesn't show up in the file manager, and the program I give the path to (via an intent) can't find it either. This is what I'm trying...
private void CopyAssets() throws IOException {
String path = Environment.getExternalStorageDirectory() + "/jazz.pdf";
InputStream in = getResources().openRawResource(R.raw.jazz);
FileOutputStream out = new FileOutputStream(path);
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
out.close();
}
}
After which I try...
try {
CopyAssets();
} catch (IOException e1) {
e1.printStackTrace();
}
String aux = Environment.getExternalStorageDirectory() + "/jazz.pdf";
Uri path = Uri.parse(aux);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(bgn1.this, "No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
Just write out.flush(); in finally block and let me know what happen,
finally {
out.flush();
in.close();
out.close();
}
Update:
Working code:
private void CopyAssets() throws IOException
{
InputStream myInput = getResources().openRawResource(R.raw.jazz);
String outFileName = Environment.getExternalStorageDirectory() + "/jazz.pdf";
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the input file to the output file
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0)
{
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
}

Categories

Resources