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.
Related
I have to open pdf file stored in asset folder. When I am trying to open the file in Android 6, it shows error. But, no problem with other android versions. I think its the problem with permission. Please help to rectify this error.
Here is my code
....
dialBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
File fileBrochure = new File(Environment.getExternalStorageDirectory() + "/" + email+".pdf");
if (!fileBrochure.exists())
{
CopyAssetsbrochure();
}
/** PDF reader code */
File file = new File(Environment.getExternalStorageDirectory() + "/" + email+".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(getApplicationContext(), "Please install any pdf reader App.",
Toast.LENGTH_LONG).show();
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.adobe.reader")));
}
}
});
}
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(email+".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);
}
}
Did you included: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
and: <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> in the manifest?
Also I have found this code:
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(), "abc.pdf");
try
{
in = assetManager.open("abc.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() + "/abc.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 link of another answer: Read a pdf file from assets folder
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
consider i have 10 images in assets folder and i retrieve it to Image View.And i have added another 10 more images to assets folder.Will i be able to view all 20 images on image view?If not why?If so how?
This is the code that i used to read and copy assets file to SD card
String[] getImagesFromAssets() {
AssetManager assetManager = getAssets();
String[] img_files = null;
try {
// img_files = getAssets().list("pictures");
img_files = assetManager.list("pictures");
} catch (IOException ex) {
Logger.getLogger(GameActivity.class
.getName()).log(Level.SEVERE, null, ex);
}
return img_files;
}
void loadImage(String name) {
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open("pictures/" + name);
File outFile = new File(getExternalFilesDir(null)+"/"+"pictures/"+ name);
out = new FileOutputStream(outFile);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
Bitmap myBitmap = BitmapFactory.decodeFile(outFile.getAbsolutePath());
image.setImageBitmap(myBitmap);
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + "pictures/" + name, e);
return;
}
}
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);
}
}
I recommend use 3rd party libs, Use Acceptable URIs examples in android Android-Universal-Image-Loader
Ex: String imageUri = "assets://image.png";
Credits: Sergey Tarasevich
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);
}
}
Can anyone kind enough show me how to copy files from my app assets folder to /system folder? I know how to get root access and all. For example: I want to copy file from "/assets/lib/libs.so" and check if this file already exist, if it does replace it to new "/system/lib/libs.so".
try this:
try {
File from = new File( "/assets/lib/libs.so" );
File to = new File( "/system/lib/libs.so" );
if( from.exists() && to.exists() ) {
FileInputStream is = new FileInputStream( from );
FileOutputStream os = new FileOutputStream( to );
FileChannel src = is.getChannel();
FileChannel dst = os.getChannel();
dst.transferFrom( src, 0, src.size() );
src.close();
dst.close();
is.close();
os.close();
}
} catch( Exception e ) {
}
This will check if your file exists, delete it and then copy everthing that is in assets.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File exists = new File("/system/lib/libs.so");
if(exists.exists()){
exists.delete();
CopyAssets();
}else{
CopyAssets();
}
}
private void CopyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
for(String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
out = new FileOutputStream("/system/lib/" + filename);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} 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);
}
}
Edit:
Declare this permission in your manifest file for filesystems.
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>