分享选定的图像只有在Android的 [英] Share the Selected Image Only in Android

查看:105
本文介绍了分享选定的图像只有在Android的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何替换 R.id.ic_launcher 与用户选择的特定的ID。 我已经创建了重新presents图像的一些网格视图的应用程序,那么用户选择后,选择特定的图像,然后共享菜单按钮,将影像分享的社会化应用。 但每当任何图像选择和共享,以只发送默认的启动器图标,即任何应用程序的错误, ic_launcher.png 。 我的code低于这样的: FullImageActivity.java

  @燮pressLint(SdCardPath)
公共类FullImageActivity延伸活动{


@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.full_image);

    意图I = getIntent();

    。INT位置= i.getExtras()调用getInt(ID);
    ImageAdapter imageAdapter =新ImageAdapter(本);

    ImageView的ImageView的=(ImageView的)findViewById(R.id.full_image_view);
    imageView.setImageResource(imageAdapter.mThumbIds [位置]);

}

@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    。getMenuInflater()膨胀(R.menu.main,菜单);
    返回true;
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //处理项目选择
    点阵位图= BitmapFactory.de codeResource(getResources(),R.drawable.ic_launcher);
    文件SD = Environment.getExternalStorageDirectory();
    字符串文件名=test.png;
    文件DEST =新的文件(SD,文件名);
    尝试 {
        FileOutputStream中出;
        OUT =新的FileOutputStream(DEST);
        bitmap.com preSS(Bitmap.Com pressFormat.PNG,100,出);
        了out.flush();
        out.close();
    }赶上(FileNotFoundException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(IOException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
    开关(item.getItemId()){
        案例R.id.item:
            开放的我们的uri = Uri.fromFile(DEST);
            意图shareIntent =新意图();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM,URI);
            shareIntent.setType(为image / jpeg);
            startActivity(Intent.createChooser(shareIntent,getResources()的getText(R.string.share))。);
            返回true;
        默认:
            返回super.onOptionsItemSelected(项目);
    }
}
 

} 这就是我实现了份额的方法,这是希尔我的code发送defautl ic_launcher.png ,而不是选定的图像。 另外我一直在所有的图像 .JPEG 格式的 ImageAdapter.java 类。

 私人语境mContext;

//保持在阵列中的所有图片
公共整数[] mThumbIds = {

        R.drawable.rage_0001,R.drawable.rage_0002,
 

和我的 AndroidManifest.xml中是这样对所有使用权限和活动记录。

 <使用-SDK
    安卓的minSdkVersion =8
    机器人:targetSdkVersion =17/>

<使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
<使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>
<使用-权限的Andr​​oid:名称=android.permission.READ_EXTERNAL_STORAG​​E/>
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_NETWORK_STATE/>

<应用
    机器人:allowBackup =真
    机器人:图标=@可绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:权限=android.permission.WRITE_EXTERNAL_STORAG​​E
    机器人:主题=@风格/ AppTheme>
    <活动
        机器人:名称=com.jai.de​​simeme.MainActivity
        机器人:标签=@字符串/ APP_NAME>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.MAIN/>

            <类机器人:名称=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.SEND/>

            <类机器人:名称=android.intent.category.DEFAULT/>

            <数据机器人:MIMETYPE =图像/ */>
        &所述; /意图滤光器>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.SEND/>

            <类机器人:名称=android.intent.category.DEFAULT/>

            <数据机器人:MIMETYPE =text / plain的/>
        &所述; /意图滤光器>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.SEND_MULTIPLE/>

            <类机器人:名称=android.intent.category.DEFAULT/>

            <数据机器人:MIMETYPE =图像/ */>
        &所述; /意图滤光器>
    < /活性GT;
    &所述;! -  FullImageActivity  - >
    <活动机器人:名称=com.jai.de​​simeme.FullImageActivity>
    < /活性GT;
    <活动
        机器人:名称=com.jai.de​​simeme.About
        机器人:主题=@安卓风格/ Theme.Dialog>
    < /活性GT;
    <活动
        机器人:名称=com.jai.de​​simeme.ShareActivity
        机器人:标签=@字符串/ title_activity_share>
    < /活性GT;
< /用途>
 

告诉我在哪里,我需要修改我的$ C $下正常工作,因为我上面提到的。

解决方案

 位图位= BitmapFactory.de codeResource(getResources(),drawable.ic_launcher); //发射是保存到文件
文件SD = Environment.getExternalStorageDirectory();
字符串文件名=test.png;
文件DEST =新的文件(SD,文件名);
尝试 {
    FileOutputStream中出;
    OUT =新的FileOutputStream(DEST);
    bitmap.com preSS(Bitmap.Com pressFormat.PNG,100,出);
    了out.flush();
    out.close();
}赶上(FileNotFoundException异常E){
    // TODO自动生成的catch块
    e.printStackTrace();
}赶上(IOException异常E){
    // TODO自动生成的catch块
    e.printStackTrace();
}
 

这是部分地方要保存的启动映像文件,从而分享这些图片

HOW CAN I REPLACE THE R.id.ic_launcher with the specific id that user selected. i have created an application which represents some grid view of images, then after user selection it select the specific image and then a share menu button share the image to the social apps. but the error is whenever any image is selected and shared to any application it only sends the default launcher icon i.e, ic_launcher.png. my code goes below like this: FullImageActivity.java

@SuppressLint("SdCardPath")
public class FullImageActivity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_image);

    Intent i = getIntent();

    int position = i.getExtras().getInt("id");
    ImageAdapter imageAdapter = new ImageAdapter(this);

    ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
    imageView.setImageResource(imageAdapter.mThumbIds[position]);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    File sd = Environment.getExternalStorageDirectory();
    String fileName = "test.png";
    File dest = new File(sd, fileName);
    try {
        FileOutputStream out;
        out = new FileOutputStream(dest);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    switch (item.getItemId()) {
        case R.id.item:
            Uri uri = Uri.fromFile(dest);
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
            shareIntent.setType("image/jpeg");
            startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share)));
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

} this is where i implemented the share method, this is shere my code sending the defautl ic_launcher.png instead of selected image. further i kept all the images in .jpeg format in ImageAdapter.java class.

private Context mContext;

// Keeping all Images in array
public Integer[] mThumbIds = {

        R.drawable.rage_0001,R.drawable.rage_0002,

and my AndroidManifest.XML goes like this for all uses permissions and activity records.

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:permission="android.permission.WRITE_EXTERNAL_STORAGE"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.jai.desimeme.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="image/*" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="text/plain" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND_MULTIPLE" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="image/*" />
        </intent-filter>
    </activity>
    <!-- FullImageActivity -->
    <activity android:name="com.jai.desimeme.FullImageActivity" >
    </activity>
    <activity
        android:name="com.jai.desimeme.About"
        android:theme="@android:style/Theme.Dialog" >
    </activity>
    <activity
        android:name="com.jai.desimeme.ShareActivity"
        android:label="@string/title_activity_share" >
    </activity>
</application>

tell me where i need to modify my code for proper working as i mentioned above.

解决方案

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),drawable.ic_launcher);//launcher  being saved to file
File sd = Environment.getExternalStorageDirectory();
String fileName = "test.png";
File dest = new File(sd, fileName);
try {
    FileOutputStream out;
    out = new FileOutputStream(dest);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
    out.flush();
    out.close();
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

This is the part where you are saving the launcher image to a file and hence sharing that image

这篇关于分享选定的图像只有在Android的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆