我正在尝试将图像发送到我的API.但是在使用MultipartEntity StringBody时我得到错误,因为不推荐使用StringBody(String).
我不工作.我是Android的一个示例,通过MultipartEntity将图像发送到服务器 - 设置内容类型?.
这是代码:
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("api_key", api_key));
params.add(new BasicNameValuePair("access_token", access_token));
API api = new API(mApiKey, mApiSecret);
HttpClient client = new DefaultHttpClient();
HttpPost postMethod = new HttpPost("MY API URL");
File file = new File(imagePath);
MultipartEntity entity = new MultipartEntity();
FileBody contentFile = new FileBody(file);
StringBody api_key = new StringBody(mApiKey);
StringBody access_token = new StringBody(access_token);
entity.addPart("api_key", api_key);
entity.addPart("hash", access_token);
entity.addPart("image", contentFile);
postMethod.setEntity(entity);
client.execute(postMethod);
} catch (Exception e) {
e.printStackTrace(); …Run Code Online (Sandbox Code Playgroud) 我正在使用一个fragement,我需要获取我当前的位置(lat和lng),我需要计算我目前位置的当前位置.
你能帮我一臂之力吗?计算位置的最佳方法是什么:
如何使用lat和lng获取当前位置和目的地位置的距离;
LocationManager locationManager;
Location locatio;
locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
String product_latitude = "3.121191";
String product_longtitude = "101.673298";
Double latitude = Double.valueOf(product_latitude);
Double longtitude = Double.valueOf(product_longtitude);
// Calculate the Distance
String product_distance = location.distanceTo(latitude, longtitude);
Run Code Online (Sandbox Code Playgroud) 我正在使用新的Android设计导航抽屉.我想在抽屉里添加一个开关.有没有实现这个?
这是菜单xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="@+id/nav_view_menu_group_1"
android:checkableBehavior="single">
<item
android:id="@+id/nav_view_menu_item_myschedule"
android:icon="@drawable/ic_navview_my_schedule"
android:title="@string/navview_menu_item_myschedule"
android:titleCondensed="@string/navview_menu_item_myschedule" />
<item
android:id="@+id/nav_view_menu_item_iolive"
android:icon="@drawable/ic_navview_play_circle_fill"
android:title="@string/navview_menu_item_iolive"
android:titleCondensed="@string/navview_menu_item_iolive"
android:visible="false"/>
<item
android:id="@+id/nav_view_menu_item_explore"
android:icon="@drawable/ic_navview_explore"
android:title="@string/navview_menu_item_explore"
android:titleCondensed="@string/navview_menu_item_explore" />
<item
android:id="@+id/nav_view_menu_item_map"
android:icon="@drawable/ic_navview_map"
android:title="@string/navview_menu_item_map"
android:titleCondensed="@string/navview_menu_item_map"
android:visible="false"/>
</group>
</menu>
Run Code Online (Sandbox Code Playgroud)
如何更改一个
<item>切换:
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Switch"
android:id="@+id/switch1"
android:layout_gravity="center_horizontal" />
Run Code Online (Sandbox Code Playgroud)
我目前正在使用默认布局:
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/activity_main_drawer" />
Run Code Online (Sandbox Code Playgroud)
就像Android Notification下的这张图片一样http://i.stack.imgur.com/M9nD7.png

我真的很感谢任何反馈.谢谢.
java android android-layout navigation-drawer material-design
我有一个应用程序从网络上下载内容.音乐,视频,pdfs ......就像一个下载管理器.
但现在每次下载内容都会崩溃:
E/LVN/advanced_memory_manager.c: ---------------------------------- AMM report ------------------------------
-> Memory Currently Allocated: 0 bytes <=> 0 components
-> Max Memory Need: 512000 bytes
-> Overall Memory Allocation: 515652 bytes (l:423)
E/art: Throwing OutOfMemoryError "Failed to allocate a 2060 byte allocation with 16777232 free bytes and 308MB until OOM; failed due to fragmentation (required continguous free 4096 bytes where largest contiguous free 0 bytes)"
E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.rokki.life2, PID: 32171
java.lang.OutOfMemoryError: Failed to allocate a 2060 byte allocation …Run Code Online (Sandbox Code Playgroud) 宣布推出适用于应用程序小部件的 Jetpack Glance Alpha。但是,圆角不适用于低于 31 (Android 12) 的 SDK。理想情况下,您希望以编程方式更改圆角的背景颜色。
有人知道解决此问题的方法吗?
我一直在尝试使用两种方法按名称以编程方式连接到开放的 wifi(连接门户)。
我使用了 op1,但它只能在 80% 的情况下工作。
操作1:
public static void connectToWifi(Context context, String networkSSID) {
WifiConfiguration conf = new WifiConfiguration();
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for (WifiConfiguration i : list) {
Log.d(TAG + " |connectToWifi()", "i.SSID=> " + i.SSID);
if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
Log.d(TAG + " |connectToWifi()", "IF > i.SSID=> " + i.SSID);
break;
}else {Log.e(TAG + " |connectToWifi()", "ELSE - FAILED …Run Code Online (Sandbox Code Playgroud) 我正在使用Volley进行API调用.我需要将图像发布到我的服务器上.
我尝试了很多MultipartRequest实现,无效.
我刚尝试使用如何通过AZ_ 在Android中使用Volley发送"multipart/form-data"POST中的示例.
但我得到MultipartRequest.getBody:IOException写入ByteArrayOutputStream错误.
你可以帮我解决我的代码,或者知道使用Volley上传图像的任何完整示例.谢谢.
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.util.CharsetUtils;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyLog;
//Code by:
//https://stackoverflow.com/questions/16797468/how-to-send-a-multipart-form-data-post-in-android-with-volley
//AZ_
//
/**
* Problems : E/Volley? [17225] MultipartRequest.getBody: IOException writing to ByteArrayOutputStream
*/
public class MultipartRequest extends Request<String> {
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
HttpEntity httpentity;
private String FILE_PART_NAME = …Run Code Online (Sandbox Code Playgroud) 我正在构建一个用于自助服务终端模式的应用程序.该设备没有root权限.该应用程序是设备管理员(android.permission.BIND_DEVICE_ADMIN).我希望该应用能够下载并直接安装其他apks到平板电脑,而无需征得许可.
我正在使用Android DownloadManager下载apk:然后:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(filePath)), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
DownloadManagerActivity.this.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
要安装apk.但这会带来一个确认弹出窗口.是下载后直接安装apk的另一个吗?谢谢
更新:
我发现了这篇文章:https: //paulononaka.wordpress.com/2011/07/02/how-to-install-a-application-in-background-on-android/
但它不起作用,而且非常有限.
有许多库可用,但哪个更适合图像上传?有谁知道Instagram使用哪个库?
我之前尝试过截击,但这并不好.
这是流行的"
AndroidAsync - https://github.com/koush/AndroidAsync
android-async-http - https://github.com/loopj/android-async-http
okhttp - https://github.com/square/okhttp
http-request - https://github.com/kevinsawicki/http-request
android-volley - https://github.com/mcxiaoke/android-volley
离子 - https://github.com/koush/ion
改造 - https://github.com/square/retrofit
Bolts-Android - https://github.com/BoltsFramework/Bolts-Android
谢谢
在我的Android应用程序中,我使用两个库,每个库都有自己的jniLibs.
库1 - https://github.com/tekinarslan/AndroidPdfViewer
图书馆2 - 自定义视频播放器sdk
该应用程序将编译和构建正常.
两个库都将jniLibs放在正确的位置.
似乎只有一个库可以工作,在本例中是pdfView.库2被称为.它打破了,日志说它无法为它加载jniLibs.
有没有人遇到过它?谢谢
chaeyoungx反馈后更新代码:
App Build.gradle:
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.my.app"
minSdkVersion 17
targetSdkVersion 22
versionCode 14
versionName "1.0"
ndk { abiFilters "armeabi", "armeabi-v7a", "arm64-v8a", "x86", "mips" }
}
....
Run Code Online (Sandbox Code Playgroud)
我在两个文件中尝试了以下代码:gradle.properties(Project and Global)
android.useDeprecatedNdk=true
Run Code Online (Sandbox Code Playgroud)
还是有同样的问题.
java-native-interface android gradle android-ndk android-studio
在我的Android XML图形布局中,我无法显示我的ToggleButton.我收到一些错误(见下文).此外,我无法使用图形布局编辑任何其他内容.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.thunder4apps.lanterna.corinthians.MainActivity" >
<ToggleButton
android:id="@+id/togglebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Vibrate on"
android:textOff="Vibrate off"
android:onClick="onToggleClicked"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
错误:
Exception raised during rendering: -1
Exception details are logged in Window > Show View > Error LogThe graphics preview in the layout editor may not be accurate:
Different corner sizes are not supported in Path.addRoundRect. (Ignore for this session)
Path.isConvex is not supported. (Ignore for this session)
Run Code Online (Sandbox Code Playgroud) 我想从我的字符串中删除[]
串食品 - 退货
[ apple, banana, orange ]
Run Code Online (Sandbox Code Playgroud)
我只想拥有:苹果,香蕉,橙子
多谢你们
如何删除之后的所有字符/数字.在一个字符串中
String i = "154.232";
我只是想 154
谢谢
我的代码:
distance = crntLocation.distanceTo(newLocation)/1000; // in km
double newKB = Math.floor(distance);
String product_distance = String.valueOf(newKB);
product_distance.replaceAll("\\..*", "");
Run Code Online (Sandbox Code Playgroud)