我开发了一个允许在Instagram中共享图像的应用程序。应用程序在执行时没有错误但我无法在Instagram中共享/上传图像。我无法弄清楚它出了什么问题。有人可以帮我弄这个吗。我的编纂如下:
MainActivity.java:
package com.example.dothis.instagram;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import java.io.File;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// listeners of our two buttons
View.OnClickListener handler = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnConnect:
createInstagramIntent();
break;
}
}
};
// our buttons
findViewById(R.id.btnConnect).setOnClickListener(handler);
}
private void createInstagramIntent() {
Intent share = new Intent(Intent.ACTION_SEND);
// If you want to share a png image only, you can do:
// setType("image/png"); OR for jpeg: setType("image/jpeg");
share.setType("image/*");
// Make sure you put example png image named myImage.png in your
// directory
String imagePath = Environment.getExternalStorageDirectory()
+ "/myImage.png";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
// Broadcast the Intent.
startActivity(Intent.createChooser(share, "Share to"));
}
}
ApplicationData.java:
package com.example.dothis.instagram;
import android.app.Activity;
public class ApplicationData extends Activity {
public static final String CLIENT_ID = "###########";
public static final String CLIENT_SECRET = "########";
public static final String CALLBACK_URL = "instagram:http://######/";
}
的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dothis.instagram" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses- permission>
</manifest>
的strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Instagram OAuth</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
</resources>
build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.dothis.instagram"
minSdkVersion 16
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
任何建议/指导都会非常有帮助。谢谢
答案 0 :(得分:1)
请试试这个。
private void postinstagraminbackground() {
// TODO Auto-generated method stub
String type = "image/*";
Intent intent = getPackageManager().getLaunchIntentForPackage(
"com.instagram.android");
if (intent != null) {
try {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setPackage("com.instagram.android");
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (file != null) {
Uri uri = Uri.fromFile(file);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
} else if (image_value != null) {
//incase of image url
shareIntent.putExtra(Intent.EXTRA_STREAM, image_value);
}
shareIntent.setType(type);
shareIntent.putExtra(Intent.EXTRA_TEXT, mfinalmsg);
// Broadcast the Intent.
startActivity(shareIntent);
// startActivity(Intent.createChooser(shareIntent,
// "Share to"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
// bring user to the market to download the app.
// or let them choose an app?
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id="
+ "com.instagram.android"));
startActivity(intent);
}
}