Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create File object from assets folder?

I am trying to read a pdf file from my assets folder but I do not know how to get the path of pdf file. I right click on pdf file and select "copy Path" and paste itenter image description here

Here is the another screen shot of my code: enter image description here

Here is my code:

File file = new File("/Users/zulqarnainmustafa/Desktop/ReadPdfFile/app/src/main/assets/Introduction.pdf");

    if (file.exists()){
        Uri path = Uri.fromFile(file);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(path, "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        try {
            this.startActivity(intent);
        }
        catch (ActivityNotFoundException e) {
            Toast.makeText(this, "No application available to view PDF", Toast.LENGTH_LONG).show();
        }
    }else{
        Toast.makeText(this, "File path not found", Toast.LENGTH_LONG).show();
    }

I always get file not found, Help me to create File object or let me know how I can get the exact path for a file I also tried with file:///android_asset/Introduction.pdf but no success. I also tried with Image.png but never gets file.exists() success. I am using Mac version of Android studio. Thanks

like image 580
Zulqarnain Mustafa Avatar asked Dec 27 '16 06:12

Zulqarnain Mustafa


3 Answers

get input stream from asset and convert it to a file object.

File f = new File(getCacheDir()+"/Introduction.pdf");
if (!f.exists()) 
try {

  InputStream is = getAssets().open("Introduction.pdf");
  byte[] buffer = new byte[1024];
  is.read(buffer);
  is.close();


  FileOutputStream fos = new FileOutputStream(f);
  fos.write(buffer);
  fos.close();
} catch (Exception e) { throw new RuntimeException(e); }
like image 56
Randyka Yudhistira Avatar answered Nov 08 '22 10:11

Randyka Yudhistira


Can you try this code

AssetManager am = getAssets();
InputStream inputStream = am.open("Indroduction.pdf");
File file = createFileFromInputStream(inputStream);

private File createFileFromInputStream(InputStream inputStream) {

   try{
      File f = new File("new FilePath");
      OutputStream outputStream = new FileOutputStream(f);
      byte buffer[] = new byte[1024];
      int length = 0;

      while((length=inputStream.read(buffer)) > 0) {
        outputStream.write(buffer,0,length);
      }

      outputStream.close();
      inputStream.close();

      return f;
   }catch (IOException e) {
         //Logging exception
   }

   return null;
}

Then try with

File file = new File("new file path");

if (file.exists())
like image 3
Mayur Raval Avatar answered Nov 08 '22 08:11

Mayur Raval


Short converter (storing asset as cache file) in Kotlin:

fun fileFromAsset(name: String) : File =
    File("$cacheDir/$name").apply { writeBytes(assets.open(name).readBytes()) }

cacheDir is just shorthand for this.getCacheDir() and should be predefined for you.

like image 5
xjcl Avatar answered Nov 08 '22 09:11

xjcl