1

私はウェブ開発者で、数週間前に Android Studio とアプリ開発者を学び始めました。私は現在、Android アプリにkii クラウドを使用しており、現在、次のシナリオを実装する方法を理解しようとしています -

を。すでにユーザー登録をしているので、現在「プロフィール編集」ページを作成中です。

b. プロファイルの編集ページで、ユーザーがボタンをクリックすると、ユーザーが画像をアップロードできるフラグメントが開くようにボタンを作成しようとしています。リンク

c. 次に、その画像を URL に添付し、プロフィール ページの表示など、必要な場所で URL を使用します。リンクここにリンクの説明を入力

EditUserProfile.java

public class EditUserProfile extends Activity {
    Button picbutton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        picbutton = (Button) findViewById(R.id.MypicButton);

        // Capture button clicks
        picbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {

        FragmentManager fm = getFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragcon);
        if(fragment == null)
            fragment = new ProfilePic();
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.fragcon, fragment);
        ft.commit();
            }
        });
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

ProfilePic.java (フラグメント)

public class ProfilePic extends Fragment {
    private static final String TAG = "ProfilePic";
    String objectUri = null;
    private static final int PICK_IMAGE = 1;
    private Activity activity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_attach_file, container,
                false);
        Bundle args = getArguments();
        objectUri = args.getString("object_uri");
        Button attachButton = (Button) view
                .findViewById(R.id.attach_file_button);
        attachButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onAttachFileButtonClicked(v);
            }
        });
        setPageImage(3);
        ImageView imageView = (ImageView) view.findViewById(R.id.details);
        imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        this.activity = null;
    }

    public void onAttachFileButtonClicked(View v) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(
                Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK) {
            Uri selectedFileUri = data.getData();
            String filePath = getFilePathByUri(selectedFileUri);
            Log.v(TAG, "Picture Path : " + filePath);
            if (filePath == null) {
                showAlert("File not exists, Please select an image that exists locally.");
                return;
            }
            uploadFile(filePath);
        } else {
            showToast("picking file failed!");
        }
    }

    private void showToast(String message) {
        Toast.makeText(this.activity, message, Toast.LENGTH_SHORT).show();
    }

    private String getFilePathByUri(Uri selectedFileUri) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            // Workaround of retrieving file image through ContentResolver
            // for Android4.2 or later
            String filePath = null;
            FileOutputStream fos = null;
            try {
                Bitmap bmp = MediaStore.Images.Media.getBitmap(
                        this.activity.getContentResolver(), selectedFileUri);

                String cacheDir = Environment.getExternalStorageDirectory()
                        .getAbsolutePath() + File.separator + "app";
                File createDir = new File(cacheDir);
                if (!createDir.exists()) {
                    createDir.mkdir();
                }
                filePath = cacheDir + File.separator + "upload.jpg";
                File file = new File(filePath);

                fos = new FileOutputStream(file);
                bmp.compress(CompressFormat.JPEG, 95, fos);
                fos.flush();
                fos.getFD().sync();
            } catch (Exception e) {
                filePath = null;
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (Exception e) {
                        // Nothing to do
                    }
                }
            }
            return filePath;
        } else {
            String[] filePathColumn = { MediaStore.MediaColumns.DATA };
            Cursor cursor = this.activity.getContentResolver().query(
                    selectedFileUri, filePathColumn, null, null, null);

            if (cursor == null)
                return null;
            try {
                if (!cursor.moveToFirst())
                    return null;
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                if (columnIndex < 0) {
                    return null;
                }
                String picturePath = cursor.getString(columnIndex);
                return picturePath;
            } finally {
                cursor.close();
            }
        }
    }

    private void uploadFile(String path) {
        KiiObject object = KiiObject.createByUri(Uri.parse(objectUri));
        File f = new File(path);
        Log.v(TAG, "file can read : " + f.canRead());
        KiiUploader uploader = object.uploader(this.activity, f);
        uploader.transferAsync(new KiiRTransferCallback() {

            @Override
            public void onStart(KiiRTransfer operator) {
                setFragmentProgress(View.VISIBLE);
            }

            @Override
            public void onTransferCompleted(KiiRTransfer operator, Exception e) {
                setFragmentProgress(View.INVISIBLE);
                if (e == null) {

                } else {

                }
            }
        });
    }

    public void moveFromDialogFragment(Class<?> clazz) {
        if (clazz != null) {
            Intent i = new Intent(this.activity, clazz);
            startActivity(i);
        }
    }

    void showAlert(String message) {
        DialogFragment newFragment = AlertDialogFragment.newInstance(
                R.string.operation_failed, message, null);
        newFragment.show(getFragmentManager(), "dialog");
    }

    void setFragmentProgress(int v) {
        ProgressFragment fragment = (ProgressFragment) getFragmentManager()
                .findFragmentById(R.id.progressFragment);
        if (fragment != null && fragment.isInLayout()) {
            fragment.setProgressBarVisiblity(v);
        }
    }

    void setPageImage(int page) {
        ProgressFragment fragment = (ProgressFragment) getFragmentManager()
                .findFragmentById(R.id.progressFragment);
        if (fragment != null && fragment.isInLayout()) {
            fragment.setPageImage(page);
        }
    }
}

次に、このようなことのために、ユーザーに関連付けられたプロフィール写真を動的に表示する方法を見つけようとしています。そうでない場合は、アイコンをデフォルトとして使用します。

holder.icon.setImageResource(R.drawable.list_account);

UserListLoader.java

public class UserListAdapter extends AbstractArrayAdapter<IUser> {

    private final LayoutInflater inflater;

    public UserListAdapter(Context context) {
        super(context, R.layout.image_list_item);
        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = this.inflater.inflate(R.layout.image_list_item, parent, false);
            holder = new ViewHolder();
            holder.text = (TextView)convertView.findViewById(R.id.row_text);
            holder.icon = (ImageView)convertView.findViewById(R.id.row_icon);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        IUser user = this.getItem(position);
        holder.position = position;
        holder.text.setText(user.getUsername());
        holder.icon.setImageResource(R.drawable.list_account);
        return convertView;
    }

}

また、アプリで開くと、EditUserProfile が picbutton.setOnClickListener(new View.OnClickListener())でクラッシュします。

この問題に関するガイダンスやヘルプをいただければ幸いです。ありがとう。

4

1 に答える 1

0

これが誰かを助けたことを願っています...

public void uploadImage() {

        UserFields userFields = new UserFields();
        userFields.putDisplayName("AllDeals");

        KiiUser.registerAsPseudoUser(userFields,
                new KiiUserRegisterCallback() {

                    @Override
                    public void onRegisterCompleted(KiiUser kiiUser,
                            Exception exception) {
                        // TODO Auto-generated method stub
                        if (exception != null) {
                            // Error handling
                            Log.e("Exception", exception.toString());
                            return;
                        } else {
                            String accessToken = kiiUser.getAccessToken();

                            final KiiObject object = Kii.bucket("AllDeals")
                                    .object();

                            object.set("placeImage", imageURL);

                            File localFile = new File(picturePath);
                            KiiUploader uploader = object.uploader(
                                    getApplicationContext(), localFile);

                            uploader.transferAsync(new KiiRTransferCallback() {
                                public void onTransferCompleted(
                                        KiiRTransfer operator,
                                        Exception exception) {
                                    if (exception != null) {
                                        return;
                                    }
                                    object.publishBody(new KiiObjectPublishCallback() {

                                        @Override
                                        public void onPublishCompleted(
                                                String url,
                                                KiiObject object,
                                                Exception exception) {
                                            if (exception != null) {
                                                return;
                                            }
                                            object.set("url", url);
                                            object.save(new KiiObjectCallBack() {
                                                public void onSaveCompleted(
                                                        int token,
                                                        KiiObject object,
                                                        Exception exception) {
                                                    if (exception != null) {
                                                        return;
                                                    }

                                                    Toast toast = Toast
                                                            .makeText(
                                                                    MainActivity.this,
                                                                    "Your image is added successfully",
                                                                    500);

                                                };
                                            });
                                        }
                                    });
                                };
                            }); 
                        }

                });

}
于 2016-10-12T11:24:39.427 に答える