I need to save page from webview then open it in webview. I found method saveWebArchive and wrote code:
Timestamp tm = new Timestamp(new Date().getTime());
String fileName = String.format("web_file_%d.mht",tm.getTime());
String path = dir.toString() + File.separator + fileName;
view.getWebView().saveWebArchive(path);
then I load page where model.getContentPath() is path from code above
webView.loadUrl("file:///"+model.getContentPath());
and I got page without CSS styels etc...
Can you say how to save and load page which will be looks like original from web?
Related
i want to open a .mp4 file, so that i can access each frame.
I tried to do it using javacv, but it didnt work.(see code below)
Does anyone know why it didnt work or how else i could achieve my goal?
i tried to call
string path = Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator + "Test";
opencv_videoio.VideoCapture cap = new opencv_videoio.VideoCapture(path + File.separator + "test.mp4");
but then cap.isOpend() returns false. I have set the permission WRITE_EXTERNAL_STORAGE(which includes READ)
We want to display html page with its contents like text, images etc offline(without internet connection) in webview. We are able to display text only. For image, we are storing image on internal storage(sd card) from image url and replacing that image url(server url) with the image internal storage(sd card) path.
But, those images are not displaying in webview.
For example,
Below is the img tag in html..
<img alt="img" class="center_top_img" src="http://test.com/uploads/section/images/523.jpg" />
We are replacing above image url(server url) with the image internal storage(sd card) path like
<img alt="img" class="center_top_img" src=\"file:///data/data/com.app.test/files/523.jpg\" />
Wherever the image is, just get its absolute path and add "file://" in advance.
File file = ...
String imagePath + "file://" + file.getAbsolutePath();
String html = "...<img src=\""+ imagePath + "\">...";
Try this
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
String imagePath = "file://"+ base + "/test.jpg";
String html = "<html><head></head><body><img src=\""+ imagePath + "\"></body></html>";
mWebView.loadDataWithBaseURL("", html, "text/html","utf-8", "");
Remember you image is inside app directory.
You can use this:
<img src="file:///data/data/com.yourapp/files/yourimage.jpg" />
In my case I wanted the images to be in 'internal storage', not the sd-card as you stated in description (and also inferred from your usage of getExternalStorageDirectory - your question title however mentioned 'internal storage'. For this reason, get the right path:
//image paths; 'images' folder was earlier created by getFilesDir
String basePATH = "";
String imagePSPPath = "";
String imagePSP = "passportImage.jpeg"; //can be generated dynamically if needed
File images = new File(getApplicationContext().getFilesDir(), "images");
if(file.exists()) {
//fine, it exists, build right strings
basePATH = images.toString();
imagePSPPath = basePATH +"/"+ imagePSP;
Log.i("XPATH", imagePSPPath); //loged for ease of copying
}else {
//error, path not found
Toast.makeText(YourActivity.this, "Sorry, path not found: ", Toast.LENGTH_LONG).show();
}
Next, use this path but also ensure that it has 3 forward slashes, not 2. For me, I just copied the XPATH address from LOG.i and pasted it in my Html page directly:
<img src="file:///"+imagePSPPath +" alt="PSP Photo">
I am downloading a pdf file from server and saving it on sd card without extension (for security purpose so that normal user can't open that file from file manager).For e.g.- I am downloading abc.pdf and saving it on sd card with abc on below path
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File folder = new File(extStorageDirectory, "files");
And later I want to view that file by adding extension.
So when I am accessing that file from code by using below code then its gives error file does't exist..
String s=Environment.getExternalStorageDirectory() + "/files/" + "abc";
File pdfFile = new File(s+".pdf");
So how can I save file in sd card without extension and later open that file with extension.
Workaround for your problem is to rename your saved pdf file without extension to filename with .pdf extension & after completing/accessing view or read operation on your new pdf extension file again you can rename it to file without extension
Example Usage:
String currentFileName = Environment.getExternalStorageDirectory() + File.separator
+ "files" + File.separator + "abc";
String renamedFilename = currentFileName + ".pdf";
boolean isRenamed = renameFile(currentFileName, renamedFilename);
Log.d("isRenamed: ", "" + isRenamed);
if(isRenamed {
//perform your operation
}
Again rename the file if not in use (here exchange renameFile() parameters):
boolean renameAfterOperation = renameFile(renamedFilename, currentFileName);
Log.d("renameAfterOperation : ", "" + renameAfterOperation );
And here is renameFile(currentFilePath, renamedFilePath):
public boolean renameFile(String currentFilePath, String renamedFilePath){
File currentFile = new File(currentFilePath);
File newFile = new File(renamedFilePath);
boolean isrenamed = currentFile.renameTo(newFile);
return isrenamed;
}
In this way whenever you want to perform operation on saved file first rename it to .pdf & whenever it's not in use, again rename it without extension. Let me know if this works for you..
I have a WebView that just displays an image from the external SD-card. This works well, however if the image is overwritten with new data, the WebView does not update the image.
This is even more curious because the WebView is created totally new in the onCreate method of the activity. Here is my code:
#Override
public void onCreate(Bundle savedInstanceState)
{
Log.d(TAG, "Creating Viewer");
super.onCreate(savedInstanceState);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String imagePath = extras.getString("imgFile");
shareImage(Uri.fromFile(new File(imagePath)));
setContentView(R.layout.viewer_test);
WebView viewer = (WebView) findViewById(R.id.imageViewer);
viewer.getSettings().setAllowFileAccess(true);
viewer.getSettings().setBuiltInZoomControls(false);
viewer.getSettings().setLoadWithOverviewMode(true);
viewer.getSettings().setUseWideViewPort(true);
viewer.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
String filename = "file://"+ imagePath;
String html = "<html><head></head><body bgcolor=\"#000000\"><center><img src=\""+ filename + "\"></center></body></html>";
viewer.loadDataWithBaseURL(null, html, "text/html","utf-8", null);
viewer.reload();
}
The image at the path that is saved in imagePath is overwritten by another activity before the path is sent to this activty. The WebView however still shows the old image data.
As you can see I already tried to set the cache mode and I tried to manually reload the WebView, with no luck unfortunately.
I also checked with a file manager and the image on the SD-card is overwritten with new data. I tried to overwrite the file multiple times, but that doesn't work either. Somehow the image data gets cached somewhere or something. How can I avoid this and load the new data every time?
(Obviously creating a new file with a different filename works fine, but I want to replace the old file.)
I'm not too familiar with Webview. but in ordinary websites you can use the querystring to emulate a unique adress, while still loading same image. this is often used on webpages on css files.
example: http://www.webpage.com/image.jpg?cachekey=23456456754
by randomizing the cachekey every time the image loaded, it is treated as a unique image and does not load from cache.
Put a randomized int or string in the end of your filename string
Random r = new Random();
int randInt = r.nextInt(8000000-1000000) + 1000000;
String query = "?cachekey=" + randInt ;
String html = "<html><head></head><body bgcolor=\"#000000\"><center><img src=\""+ filename + query "\"></center></body></html>";
I have not tested this yet. but it's an idea how to solve it.
I m stuck with an issue that I want to copy/move a video from one location to another inside sdcard in android.
does anybody know how to do that.?
private String CopyFile(String srcPath,String destPath)
{
String videoFileName = VIDEO_FILE_PREFIX + mCurrentQuestion.getQuestionId() + VIDEO_FILE_SUFFIX ;
File source= new File(srcPath);
File destination= new File(destPath+"/"+videoFileName);
source.renameTo(destination);
return destPath+"/"+videoFileName;
}