Android WebView:默认另存为上下文长按

发布于 2024-12-29 21:41:19 字数 2216 浏览 0 评论 0原文

您好,首先我想说我是编程新手(我主要处理图形设计),我想设置一个应用程序,在其中显示图像库,并且用户可以将图像保存到他们的设备中。我正在使用 WebView,因为它对我来说是最简单的,而且现在我使用 html 在应用程序中显示了一个图像。我希望用户能够长按来调用默认浏览器“另存为”或“设置为壁纸”操作。

以下是我的 java 文件中的代码:

package com.wallpapergallery.wjd;

import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.app.Activity;

public class WallpaperGallerybyWJDDesignsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    WebView webview = (WebView)findViewById(R.id.webview);     
    webview.setWebChromeClient(new WebChromeClient());
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("file:///android_asset/www/index.html");

    }
}

当然,然后打开索引页面,我在页面上放置了一个普通的 png 进行测试。在您回答之前,我警告您,我对此非常陌生,因此请解释示例代码的帮助,以便我可以学习,将不胜感激。

完成此操作后,我的想法是使用 HTML5 画廊来显示我的壁纸并允许用户保存/设置为壁纸。


更新 1: 我已经成功地让长按工作并给我返回 logcat 信息,这对我来说非常令人兴奋 - 现在我需要做的就是弹出该菜单,就像浏览器显示“保存图像”“查看图像”“设置为壁纸”-再次感谢任何帮助,请随时使用我的代码修复它并告诉我为什么要这样做,如果你让它工作>_<

新代码:

package com.wallpapergallery.wjd;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.app.Activity;

public class WallpaperGallerybyWJDDesignsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    WebView webview = (WebView)findViewById(R.id.webview);

    webview.setOnLongClickListener(listener);

    webview.setWebChromeClient(new WebChromeClient());
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("file:///android_asset/www/index.html");                        
}
private OnLongClickListener listener = new OnLongClickListener() {

    public boolean onLongClick(View v) {

       Log.i("test","worked");

       return true;

    }

 };

}

Hello first off I wanted to say that I am new into programming (I mainly deal with graphic design) and I am wanting to set up an application where I show an image gallery and the user can save the images to their device. I am using WebView since it is easiest on my and for right now I have an image displayed fine in the application using html. I would like the user to be able to long press to invoke the default browser "save as" or "set as wallpaper" actions.

Here is what I have as far as code in my java file:

package com.wallpapergallery.wjd;

import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.app.Activity;

public class WallpaperGallerybyWJDDesignsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    WebView webview = (WebView)findViewById(R.id.webview);     
    webview.setWebChromeClient(new WebChromeClient());
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("file:///android_asset/www/index.html");

    }
}

Of course then opens up the index page which I have a normal png placed on the page for testing. Before you answer I warn you I am very new to this so explained help with sample code so I can learn would be appreciated.

Once this is done my idea is to use an HTML5 gallery to show my wallpapers and allow the user to save/set as wallpaper.


Update 1: I have managed to have the long press work and give me back logcat info which is pretty exciting for me - now all I need to do is get that menu to popup like the browser does showing "Save Image" "View Image" "Set As Wallpaper" - Again any help is appreciated and please feel free to take my code fix it and teach me why you did that if you get it to work >_<

New Code:

package com.wallpapergallery.wjd;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.app.Activity;

public class WallpaperGallerybyWJDDesignsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    WebView webview = (WebView)findViewById(R.id.webview);

    webview.setOnLongClickListener(listener);

    webview.setWebChromeClient(new WebChromeClient());
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("file:///android_asset/www/index.html");                        
}
private OnLongClickListener listener = new OnLongClickListener() {

    public boolean onLongClick(View v) {

       Log.i("test","worked");

       return true;

    }

 };

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守望孤独 2025-01-05 21:41:19

I BELIEVE this is what your looking for, android set image as contact icon/wallpaper, specifically the Intent setAs = new Intent(Intent.ACTION_ATTACH_DATA);
setAs.setType("image/jpg");
part. I'm not entirely sure this is what your looking for, but if its not exactly right, it should get you on the right track.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文